一、容器
1.容器类
1) 容器类或者叫集合框架:
容器:可以存储多个数据,长度可以随内容的多少进行改变,可以存储任意类型的数据
数组:储存多个数据,数据类型相同,长度一旦确定不可改变,有序的(索引)
2) 容器中的元素类型都为引用类型,不能放置原生数据类型(使用装箱即可),使用泛型保留类型
3) <> 泛型
JDK1.4 以前类型不明确:装入集合的类型都被当作 Object 对待,从而失去自己的实际类型, 从集合中取出时往往需要转型,效率低,容易产生错误。
泛型的好处:增强程序的可读性和稳定性
4)集合框架——简单的体系结构
2.Collection 接口
Collection 接口是一组允许重复的对象
Set 接口继承 Collection,无序不允许重复,使用自己内部的一个排列机制
List 接口继承 Collection,有序允许重复,以元素安插的次序来放置元素,不会重新排列
import java
.util
.Arrays
;
public class Dome01 {
public static void main(String
[] args
) {
MyContainer con
=new MyContainer();
con
.add("钢铁侠");
System
.out
.println(con
.size());
con
.add("蜘蛛侠");
System
.out
.println(con
.size());
con
.add("蝙蝠侠");
System
.out
.println(con
.size());
System
.out
.println(con
.get(0));
System
.out
.println(con
.get(1));
System
.out
.println(con
.get(2));
System
.out
.println(con
);
con
.delete(0);
System
.out
.println(con
);
}
}
class MyContainer{
private String
[] arr
;
private int size
;
public MyContainer(){
arr
= new String[0] ;
}
public int size() {
return this.size
;
}
public String
get(int index
){
if (index
<0||index
>=size
) {
return "数组索引越界!!!";
}
return arr
[index
];
}
public void add(String value
){
String
[] temp
= arr
;
arr
=new String[size
+1];
for(int i
=0;i
<=size
-1;i
++){
arr
[i
]=temp
[i
];
}
arr
[size
]=value
;
size
++;
}
public void delete(int index
){
if (index
<0||index
>=size
) {
throw new ArrayIndexOutOfBoundsException("数组索引越界!!!");
}
String
[] temp
=arr
;
arr
=new String [size
-1];
for(int i
=0;i
<=size
-1;i
++){
if (i
>=index
) {
if (i
==index
) {
continue;
}
arr
[i
-1] =temp
[i
];
}else{
arr
[i
]=temp
[i
];
}
}
size
--;
}
@Override
public String
toString() {
return "MyContainer [arr=" + Arrays
.toString(arr
) + ", size=" + size
+ "]";
}
}
结果:
1)List 子接口
import java
.util
.ArrayList
;
import java
.util
.Arrays
;
import java
.util
.List
;
public class List01 {
public static void main(String
[] args
) {
List
<Integer> ls
=new ArrayList<Integer>();
ls
.add(6);
ls
.add(5);
ls
.add(4);
ls
.add(3);
ls
.add(2);
ls
.add(1);
ls
.add(0);
System
.out
.println(ls
);
ls
.add(5,5);
System
.out
.println(ls
);
System
.out
.println(ls
.get(2));
System
.out
.println(ls
.remove(2));
System
.out
.println(ls
);
System
.out
.println(ls
.subList(1, 4));
System
.out
.println(ls
);
System
.out
.println(Arrays
.toString(ls
.toArray()));
System
.out
.println(ls
.toArray()[0]);
for(int i
= 0;i
<=ls
.size()-1;i
++){
System
.out
.println(ls
.get(i
));
}
}
}
2)List 接口
List 接口下的实现类:有序可重复
(1)ArrayList
底层实现: 由可变数组数组实现,通过数组拷贝实现容器可以根据内容进行动态扩容
优点: 遍历和获取的时候效率高,因为数据根据索引操作效率高
缺点: 增删效率低,大量涉及到数组拷贝问题
扩容: 使用Arrays的copyOf方法进行扩容,每次扩容原容量的1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1); 新的容量是老容量的1.5倍
新增方法:没有新增方法,可以多态
应用场景:
单线程环境下,在大量做查询的业务下,适合使用ArrayList容器
import java
.util
.ArrayList
;
public class ListDemo03 {
public static void main(String
[] args
) {
ArrayList
<Person> ls
=new ArrayList();
ls
.add(new Person("张三",18));
ls
.add(new Person("李四",18));
ls
.add(new Person("王五",20));
System
.out
.println(ls
);
System
.out
.println(ls
.indexOf(new Person("张三",18)));
}
}
class Person{
private String name
;
private int age
;
public Person() {
}
public Person(String name
, int age
) {
super();
this.name
= name
;
this.age
= age
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public int getAge() {
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
@Override
public String
toString() {
return "Person [name=" + name
+ ", age=" + age
+ "]";
}
@Override
public boolean equals(Object obj
) {
if (this == obj
)
return true;
if (obj
== null
)
return false;
if (getClass() != obj
.getClass())
return false;
Person other
= (Person
) obj
;
if (age
!= other
.age
)
return false;
if (name
== null
) {
if (other
.name
!= null
)
return false;
} else if (!name
.equals(other
.name
))
return false;
return true;
}
}
(2)Vector
和ArrayList非常像
区别: a.ArrayList线程不安全,效率较高,Vector线程安全的,效率较低
b.扩容是每次扩容原容量的2倍,ArrayList1.5倍
应用场景:多线程环境下,保证数据安全,大量做查询适合使用Vector
(3)LinkedList
底层实现: 底层是由双向链表结构实现
优点: 做增删效率高
缺点: 做查询效率低
新增方法: 新增了一些有关于链表头和链表尾部的方法
import java
.util
.LinkedList
;
public class LinkedList04 {
public static void main(String
[] args
) {
LinkedList
<Character> ls
=new LinkedList();
ls
.add('s');
ls
.add('h');
ls
.add('s');
ls
.add('x');
ls
.add('t');
System
.out
.println(ls
);
ls
.removeFirst();
System
.out
.println(ls
);
}
}
3.Map 接口
Map接口是一组成对的键值对象,即所持有的是 key-value pairs。Map 中不能有重 复的 key,拥有自己的内部排列机制
待续。。。。。