1. 集合类总纲
![在这里插入图片描述](https://img-blog.csdnimg.cn/a24c8ecd273e48d284b5fc88779845b1.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBATm9CdWcg44Wk,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
2. ArrayList
2.1 性质
- List 接口,元素有顺序。
- List 继承 Collection 接口。
- ArrayList类实现 List接口。
- 可以理解为 ArrayList 为动态数组。
- 向 List 中间插入或移除元素的速度很慢。
- ArrayList 不宜进行频繁的增删。
2.2 ArrayList 构造与集合元素的读写
public static void practice1() {
List<Student> list = new ArrayList<Student>();
Student s1 = new Student("1001", "chen", 18);
Student s2 = new Student("1001", "luo", 25);
Student s3 = new Student("1003", "zhang", 25);
Student s4 = new Student("1004", "pi", 20);
Student s5 = new Student("1002", "wang", 25);
Student s6 = new Student("1001", "chen", 18);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
1001 chen 18
1001 luo 25
1003 zhang 25
1004 pi 20
1002 wang 25
1001 chen 18
2.3 通过下标遍历
for(int i=0; i<arraylist.size(); i++) {
Student s = arraylist.get(i);
System.out.println(s);
}
2.4 删除元素
arraylist.remove(0);
arraylist.remove(s2);
3. LinkedList
3.1 性质
- LinkedList 与 ArrayList 相反。
- 有序,有重复。
- 适合用来增加和移除元素。
- 但随即访问较慢。
- 可以通过 LinkedList 来实现栈 Stack 或 队列 Queue。
- LinkedList 中 addFirst(), addLast(), getFirst(), getLast(), removeFirst(), removeLast()。
3.2 集合 LinkedList 使用样例
public static void practice1() {
LinkedList<String> linkedlist = new LinkedList<String>();
linkedlist.add("a");
linkedlist.add("b");
linkedlist.add("c");
linkedlist.addFirst("x");
linkedlist.addLast("y");
for(int i=0; i<linkedlist.size(); i++) {
String str = linkedlist.get(i);
System.out.println(str);
}
}
x
a
b
c
y