0%

Java数组,String,链表

1.数组

1.声明方式(也可分布完成)

数据类型 数组名称 [] =new 数据类型[长度]
数据类型 [] 数组名称  =new 数据类型[长度]

2.初始化

2.1常见方式

数据类型 数组名称 [] ={值1,值2}

数据类型 [] 数组名称  ={值1,值2}

2.2推荐方式

数据类型 数组名称 [] =new 数据类型 []{值1,值2}

数据类型 [] 数组名称 =new 数据类型 []{值1,值2}

2.3 二维数组

数据类型 数组名称 [] []  =new  数组名称[行数][列数]

数据类型 数组名称 [] []=new 数据类型 [][]{值1,值2....}{值1,值2...}....

2.4 对象数组

类名称  对象数组名称 []=new  类名称 [长度]

类名称  对象数组名称 []=new 类名称 []{对象1,对象2}

3.数组引用内存变化

4.数组相关方法

  1. 给数组赋值:通过 fill 方法。

  2. 对数组排序:通过 sort 方法,按升序。

  3. 比较数组:通过 equals 方法比较数组中元素值是否相等。

  4. 查找数组元素:通过 binarySearch 方法能对排序好的数组进行二分查找法操作。

2.String类

1.实例化方式

  1. 直接赋值字符串的形式为String类对象实例化。

  2. 采用String类的构造方法为String类的对象实例化

    两种实例化方式对比

  3. 直接赋值:只开辟一块堆内存空间,字符串的内容可以自动入池,以供下次使用。

  4. 构造方法:开辟两块堆内存空间,有一块成为垃圾,并且不能自动入池,需要用intern()手工入池。

String str = "hello"
String str = new String("hello")

2.String类中==和equal()区别

  1. ==:比较的是两个字符串内存地址的数值是否相等,属于数值比较。

  2. equal :比较的是两个字符串的内容,属于内容比较。

  3. 字符串常见方法

    https://www.cnblogs.com/huxiuqian/p/10167415.html

4.常见方法举例

public static void main(String args[]){
String str1 = "Hello World";
String str2 = "Hello World";
String str3 = "hello world";
String str4 = " hello world ";
//字符串比较字符串比较compareTo(返回的是int),0相等,复数小于,正数大于
System.out.println("r1 : " + str1.compareTo(str2));
//字符串比较字符串比较compareTo(返回的是int),0相等,复数小于,正数大于
System.out.println("r2 : " + str1.compareTo(str3));
//字符串比较compareToIgnoreCase,忽略大小写。0相等,复数小于,正数大于
System.out.println("r3 : " + str1.compareToIgnoreCase(str3));
//字符串查找indexOf,返回的是找到的第一个的位置,没找到返回-1。从0开始
System.out.println("r4 : " + str1.indexOf("o"));
//查找字符串最后一次出现的位置lastIndexOf
System.out.println("r5 : " + str1.lastIndexOf("o"));
//删除字符串中的一个字符,字符串从0开始的 substring(a, b)返回指定起始位置(含)到结束位置(不含)之间的字符串
System.out.println("r6 : " + str1.substring(0, 5) + str1.substring(6));
//字符串替换,替换所有
System.out.println("r7 : " + str1.replace("o", "h"));
//字符串替换,替换所有
System.out.println("r8 : " + str1.replaceAll("o", "h"));
//字符串替换,替换第一个
System.out.println("r9 : " + str1.replaceFirst("o", "h"));
//字符串反转
System.out.println("r10 : " + new StringBuffer(str1).reverse());
//字符串分割
String [] temp = str1.split("\\ ");
for (String str : temp){
    System.out.println("r11 : " + str);
}
//字符串转大写
System.out.println("r12 : " + str1.toUpperCase());
//字符串转小写
System.out.println("r13 : " + str1.toLowerCase());
//去掉首尾空格
System.out.println("r14 : " + str4.trim());
//是否包含,大小写区分
System.out.println("r15 : " + str1.contains("World"));
//返回指定位置字符
System.out.println("r16 : " + str1.charAt(4));

}

5.String Stringbuffer,atringbulider相关知识

特点总结
  1. String长度大小不可变

  2. Stringbuffer和StringBuilder长度可变

  3. StringBuffer线程安全StringBuilder线程不安全

  4. StringBuilder速度快

    使用场景总结

  5. String:适用于少量的字符串操作的情况,即创建复制后,修改的比较少的情况

  6. StringBuffer:适用于单线程下字符串缓冲区进行大量操作的情况

  7. StringBuilder:适用于多线程下字符串缓冲区进行大量操作的情况

3.链表

1.定义

一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。

2.链表基本原理

3.优点

除非需要频繁通过下标随机访问各个数据,否则在很多使用数组的地方都可以用链表代替。

4.单链表举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/Link类的数据结构
class Link {
public int data;
public int idata;
public Link next;
//带参构造函数
public Link(int id, int da) {
idata=id;
data=da;
}
//显示节点的信息
public void displayLink(){
System.out.println("{"+idata+","+data+"}");
}
}
//LinkList数据结构
class ListLink{
private Link first; //对链表中第一个链结点的引用
public ListLink() {
first=null;
}
//是否为空链表
public boolean isEmpty(){
return (first==null);
}
public void insertFirst(int id,int data){
//先给新的链分配空间
Link newLink = new Link(id, data);
newLink.next = first;
first = newLink;
}
public Link deleteFirst(){
Link temp = first;
first = first.next;
return temp;
}
//第一种查找方法
public Link find\_me(int id){
Link current = first;

while(current!=null ){
if(current.idata==id)
return current;
else
current = current.next;
}
return null;
}
//第二种查找方法
public Link find(int key){
Link current = first;
while(current.idata!=key){
if(current.next==null)
return null;
else
current = current.next;
}
return current;
}
public Link delete(int key){
Link previous = first;
Link current = first;
//找到需要删除的节点
while(current.idata!=key){
if(current.next==null)
return null;
else{
previous = current; //对前一个Link的引用
current = current.next;
}
}
if(current==first) //首节点
first = first.next;
else //非首节点
previous.next=current.next;

return current;
}
//打印出LinkList
public void displayList(){
System.out.println("first--\>last");
Link current =first;
while(current!=null){
current.displayLink();
current=current.next;
}
System.out.println(" ");
}
}
//main
public class LinkList {

public static void main(String[] args) {
ListLink theList= new ListLink();
//插入链结点 头插法
theList.insertFirst(22, 299);
theList.insertFirst(44, 499);
theList.insertFirst(66, 699);
theList.insertFirst(88, 899);
theList.displayList();
//查找id为33的链结点
Link find\_id = theList.find(33);
//如果找到
if(find\_id!=null)
System.out.println("find\_id = "+find\_id.idata);
//未找到
else
System.out.println("can't find the id");
//删除id为44的链结点
Link del\_link = theList.delete(44);
//存在并且删除
if(del\_link!=null){
System.out.println("successfully deleted!");
theList.displayList();
}
//不存在
else
System.out.println("can't delete!");
}
}

5.其他知识点

https://blog.csdn.net/dingchenxixi/article/details/52423109

6.链表和数组的对比

  1. 数组静态分配内存,链表动态分配内存;

  2. 数组在内存中连续,链表不连续;

  3. 数组元素在栈区,链表元素在堆区;

  4. 数组利用下标定位,时间复杂度为O(1),链表定位元素时间复杂度O(n);

  5. 数组插入或删除元素的时间复杂度O(n),链表的时间复杂度O(1)。

7.数组和链表优缺点总结

(1)数组的优点:

   随机访问性强

   查询速度快

(2)数组的缺点:

   增删速度慢

   可能浪费内存

   内存空间要求高,必须有足够大的连续内存存储空间。

   数组的大小固定,不能动态扩展。

(3)链表的优点

   插入删除速度快

   大小不固定,可以动态扩展。

   内存利用率高,不会浪费内存

(4)链表的缺点:

    不能随机查找,必须从第一个开始遍历,查找效率低

-------------本文结束元宝感谢您的阅读-------------