前言
1.List接口继承Collection接口,实现了List接口的类称为List集合。
2.在List集合中允许出现重复的对象,所有对象以线性方式进行存储,可以通过索引来访问集合中指定的元素。List集合的元素的存储顺序和取出顺序一致。
3.List不但继承了Collection接口中的全部方法,还增加了一些根据元素位置索引来操作集合的特有方法。
一、List方法
特有的方法:
add(int index, 元素) 在指定下标位置插入元素
Object get(int index) 获取指定位置的元素
int indexOf(元素) 找到这个元素第一次出现的位置
int lastIndexOf(元素) 找到这个元素最后一次出现的位置
remove(int index ) 删除指定位置上的元素
set(int index, 新的元素) 修改指定位置上的元素
List<E> subList(int fromIndex, int toIndex) 截取指定位置上元素, 不包括结束位置
常用实现类: ArrayList, LinkedList
代码实践
public static void shuffle(List<?> list):打乱集合顺序
public class Demo01 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("A1");
list.add("A2");
list.add("A3");
list.add("A4");
list.add("A5");
System.out.println("list = " + list);
Collections.shuffle(list);
System.out.println("list = " + list);
//Set集合,
HashSet<String> set = new HashSet<>();
//Collections.shuffle(set);
}
}
如果想要使用该方法进行排序,条件是:集合中的类型T必须是Comparable的子类型。
方法:
public int compareTo(T o); //抽象方法
//子类实现后,需要进行重写
返回值int,只关心正负或者0,不需要关系具体值
原始排序:this【我】 o【他】
如果要升序排序:我减他
如果要降序排序:他-我
如果比较的属性类型是浮点型:
this.score = 99.9 o.score = 99.5
if(this.score>o.score){
return 1;
}else if(this.score<0.score){
return -1;
}else{
return 0;
}
代码实践:
String是Comparable的子类型,所以支持排序。
1.字符串作为集合中的类型进行排序
字符串排序的规则:先从字符串中的第一个字符开始比较其编码的大小,按照升序排序。如果第一个相同比较第二个,如果第二个相同比较第三个…
public class Demo01 {
public static void main(String[] args) {
//定义一个字符串的List集合,并排序
List<String> list = new ArrayList<>();
list.add("123");
list.add("213");
list.add("abc");
list.add("aac");
list.add("112");
list.add("224");
list.add("223");
list.add("Aac");
System.out.println("list = " + list);
Collections.sort(list);
System.out.println("list = " + list);
}
}
2.自定义类型实现排序
自定义学生类型含有年龄,名称属性,创建多个学生对象。放入List集合按照年龄升序排序。
public class Demo02 {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("迪丽热巴", 18));
list.add(new Student("热巴", 19));
list.add(new Student("娜扎", 16));
System.out.println("list = " + list);
Collections.sort(list);//集合中的类型需要实现Compareable接口
System.out.println("list = " + list);
}
}
class Student implements Comparable<Student> {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
//如何重写方法
//按年龄升序排序比较
//this(当前调用方法的对象) o(参数传入)
//所有比较最底层的逻辑都是发生两两比较逻辑,返回比较结果
//正数 this.age > o.age
//负数 this.age < o.age
//0 this.age==o.age
return this.age - o.age;
}
}
二、ArrayList
1.ArrayList介绍
底层使用数组实现,数据结构与数组一样,且自动扩容
构造方法
注意:
集合: 存放的元素都是对象, 如果是基本数据类型, 自动的装箱,变成包装类对象
private static final Object[] ArrayList 底层是Object[] 数组
java高级: 学的JDK提供的类, 这个类干什么的, 怎么创建这个类对象, 有哪些方法
2.数据存储
自动扩容:
elementData[] = new int[10];
添加了10个元素, 添加11个, 新创建一个数组: 长度 = 旧数组长度 * 1.5 = 10*1.5 = 15, 再把旧数组中的元素拷贝到新数组, elementData 指向新数组
ArrayList:特征:查找元素效率高,插入删除元素效率低
ArrayList 对elementData数组进行new操作, 初始化, 不是在new ArrayList() , 而是在第一次调用add() 方法
elementData[]={}
添加元素:
判断数组时候是否需要扩容
elementData的长度 < 最小容量(原来元素个数 + 1)
调用: grow(最小容量) 自动扩容
第一次添加元素:
初始化elementData = new Object[10];
这个集合已经有元素:
新数组长度 = 旧的数组长度 *1.5
进行数组的拷贝, Arrays.copyof()
往elementData添加新元素
elementData[size++] = 新元素;
3.代码实践
public static void main(String[] args) {
int[] arr = {432, 53, 6, 323, 765, 7, 254, 37, 698, 97, 64, 7};
//将数组排序
Arrays.sort(arr);
//打印数组
System.out.println(Arrays.toString(arr));
}
结果:
[6, 7, 7, 37, 53, 64, 97, 254, 323, 432, 698, 765]
三、LinkedList
1.介绍
LinkedList: 底层存储元素: 使用双向链表: 不连续的空间, 由n个节点组成, 节点就是一个对象: Node
LinkedList: 特征: 查询效率低, 插入,删除效率高