- 数组和集合最大区别: 数组不可扩展, 集合可以自动挂扩展。
- 集合的泛型不能为基本数据类型 , 只能是它们的包装类。
- 集合的两大派系: collection(单列) 、map(双列)
- collection(单列)又分为两个子接口: list(存入元素可重复)、set(存入元素不可重复)。
- map(双列)分为Hashmap和Treemap
- list接口分为ArrayList、Linkedlist两个实现类
- set接口分为HashSet、TreeSet两个实现类
- collection接口中的方法如下:
public class Test06 {
public static void main(String[] args) {
//1.子类对象指向父类引用,多态的体现
Collection<String> collection = new ArrayList<>();
//add添加方法
collection.add("王者荣耀");
collection.add("王者农药");
collection.add("和平精英");
collection.add("消消乐");
// remove根据指定值进行删除,返回值为boolean类型
boolean remove = collection.remove("王者农药");
System.out.println(remove);
//removeif参数lambda表达式
collection.removeIf(
(String s)->{
return s.length()==3;
}
);
System.out.println(collection);
//clear清除集合中所有元素,没有返回值
collection.clear();
System.out.println(collection);
//contains判断指定元素集合中是否存在,返回值为Boolean类型
boolean contains = collection.contains("王者荣耀");
System.out.println(contains);
//isEmpty判断当前集合是否为空,返回值为Boolean类型
boolean empty = collection.isEmpty();
System.out.println(empty);
//size返回当前集合元素的长度
int size = collection.size();
System.out.println(size);
}
}
- iterator()迭代器的基本使用,集合专用遍历方式
public class Test07 {
public static void main(String[] args) {
//iterator()迭代器的基本使用,集合专用遍历方式
ArrayList<String> collection = new ArrayList<>();
collection.add("王者荣耀");
collection.add("王者农药");
collection.add("和平精英");
collection.add("消消乐");
//获取迭代器对象
Iterator<String> iterator = collection.iterator();
//hasNext判断当前位置是否有元素
while (iterator.hasNext()){
//next取出当前元素并将迭代器指向下一个位置
System.out.println(iterator.next());
}
//迭代器删除集合元素
while (iterator.hasNext()){
String next = iterator.next();
//对取出的元素进行判断,若符合则调用iterator.remove();
if ("消消乐".equals(next)){
iterator.remove();
}
}
System.out.println(collection);
}
}
- 增强for循环
public class Test08 {
public static void main(String[] args) {
//增强for循环
ArrayList<String> collection = new ArrayList<>();
collection.add("王者荣耀");
collection.add("王者农药");
collection.add("和平精英");
collection.add("消消乐");
//对象名.for快捷生成增强for循环
for (String s : collection) {
//增强for循环内修改变量的值,不会影响集合中的元素
s = "刺激战场"
System.out.println(s);
}
System.out.println(collection);
}
}
- 三种循环的使用场景:
- 普通fori循环,如需操作索引则可使用
- 增强for循环,若仅仅查看结果,不做任何操作,使用增强for循环
- 迭代器,倘若遍历过程中需要用到删除操作,使用迭代器
- 迭代器以及增强for循环:单列集合可以直接使用,双列集合不可直接使用
- list集合特点:存取有序、元素可重复、可通过索引操作元素
- list特有的方法都是与索引有关
public class Test09 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
//add在指定的索引位置添加指定的元素
list.add("向日葵");
list.add("玫瑰");
list.add("牡丹");
//System.out.println(list);
list.add(2,"刺玫");
System.out.println(list);
//remove删除指定索引上的元素,并将其返回
String remove = list.remove(0);
System.out.println(remove);
//set修改指定索引上元素,并将其返回
String set = list.set(0, "寒梅");
System.out.println(set);
//get取出指定索引上的元素
String get = list.get(2);
System.out.println(get);
}
}
-
四种数据结构:
-
栈:先进后出
-
队列:先进先出
-
数组:查询快, 增删慢(ArrayList底层原理由数组组成)
-
链表:分为单双链表,增删快,查询慢,(LinkList底层由双向链表组成)
-
LinkList特有方法:
public class Test010 {
public static void main(String[] args) {
LinkedList<String> link = new LinkedList<>();
link.add("向日葵");
link.add("玫瑰");
link.add("牡丹");
//addFirst添加在首位索引,addLast添加在末尾索引
link.addFirst("梅花");
link.addLast("百合");
System.out.println(link);
//getFirst返回首位索引值,getLast返回末尾索引值
String first = link.getFirst();
System.out.println(first);
String last = link.getLast();
System.out.println(last);
//removeFirst删除首位索引并返回其元素值,removeLast删除末尾元素值并将其返回
String s = link.removeFirst();
System.out.println(s);
String s1 = link.removeLast();
System.out.println(s1);
}
}