Java_集合之一
1.Collection集合
1.1数组和集合的区别【理解】
-
相同点
都是容器,可以存储多个数据
-
不同点
-
数组的长度是不可变的,集合的长度是可变的
-
数组可以存基本数据类型和引用数据类型
集合只能存引用数据类型,如果要存基本数据类型,需要存对应的包装类
-
1.3Collection 集合概述和使用【应用】
-
Collection集合概述
-
是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素
-
JDK 不提供此接口的任何直接实现.它提供更具体的子接口(如Set和List)实现
-
-
创建Collection集合的对象
-
多态的方式
-
具体的实现类ArrayList
-
-
Collection集合常用方法
方法名 说明 boolean add(E e) 添加元素 boolean remove(Object o) 从集合中移除指定的元素 boolean removeIf(Object o) 根据条件进行移除 void clear() 清空集合中的元素 boolean contains(Object o) 判断集合中是否存在指定的元素 boolean isEmpty() 判断集合是否为空 int size() 集合的长度,也就是集合中元素的个数
1.4Collection集合的遍历【应用】
-
迭代器介绍
-
迭代器,集合的专用遍历方式
-
Iterator<E> iterator(): 返回此集合中元素的迭代器,通过集合对象的iterator()方法得到
-
-
Iterator中的常用方法
boolean hasNext(): 判断当前位置是否有元素可以被取出 E next(): 获取当前位置的元素,将迭代器对象移向下一个索引位置
-
Collection集合的遍历
1 public class IteratorDemo1 { 2 public static void main(String[] args) { 3 //创建集合对象 4 Collection<String> c = new ArrayList<>(); 5 6 //添加元素 7 c.add("hello"); 8 c.add("world"); 9 c.add("java"); 10 c.add("javaee"); 11 12 //Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到 13 Iterator<String> it = c.iterator(); 14 15 //用while循环改进元素的判断和获取 16 while (it.hasNext()) { 17 String s = it.next(); 18 System.out.println(s); 19 } 20 } 21 }
-
迭代器中删除的方法
void remove(): 删除迭代器对象当前指向的元素
1 public class IteratorDemo2 { 2 public static void main(String[] args) { 3 ArrayList<String> list = new ArrayList<>(); 4 list.add("a"); 5 list.add("b"); 6 list.add("b"); 7 list.add("c"); 8 list.add("d"); 9 10 Iterator<String> it = list.iterator(); 11 while(it.hasNext()){ 12 String s = it.next(); 13 if("b".equals(s)){ 14 //指向谁,那么此时就删除谁. 15 it.remove(); 16 } 17 } 18 System.out.println(list); 19 } 20 }
1.5增强for循环【应用】
-
介绍
-
它是JDK5之后出现的,其内部原理是一个Iterator迭代器
-
实现Iterable接口的类才可以使用迭代器和增强for
-
简化数组和Collection集合的遍历
-
-
格式
for(集合/数组中元素的数据类型 变量名 : 集合/数组名) {
// 已经将当前遍历到的元素封装到变量中了,直接使用变量即可
}
-
代码
1 public class MyCollectonDemo1 { 2 public static void main(String[] args) { 3 ArrayList<String> list = new ArrayList<>(); 4 list.add("a"); 5 list.add("b"); 6 list.add("c"); 7 list.add("d"); 8 list.add("e"); 9 list.add("f"); 10 11 //1,数据类型一定是集合或者数组中元素的类型 12 //2,str仅仅是一个变量名而已,在循环的过程中,依次表示集合或者数组中的每一个元素 13 //3,list就是要遍历的集合或者数组 14 for(String str : list){ 15 System.out.println(str); 16 } 17 } 18 }
2.List集合
2.1List集合的概述和特点【记忆】
-
List集合的概述
-
有序集合,这里的有序指的是存取顺序
-
用户可以精确控制列表中每个元素的插入位置,用户可以通过整数索引访问元素,并搜索列表中的元素
-
与Set集合不同,列表通常允许重复的元素
-
-
List集合的特点
-
存取有序
-
可以重复
-
有索引
-
2.2List集合的特有方法【应用】
方法介绍
方法名 | 描述 |
---|---|
void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
E remove(int index) | 删除指定索引处的元素,返回被删除的元素 |
E set(int index,E element) | 修改指定索引处的元素,返回被修改的元素 |
E get(int index) | 返回指定索引处的元素 |
示例代码
1 public class MyListDemo { 2 public static void main(String[] args) { 3 List<String> list = new ArrayList<>(); 4 list.add("aaa"); 5 list.add("bbb"); 6 list.add("ccc"); 7 //method1(list); 8 //method2(list); 9 //method3(list); 10 //method4(list); 11 } 12 13 private static void method4(List<String> list) { 14 // E get(int index) 返回指定索引处的元素 15 String s = list.get(0); 16 System.out.println(s); 17 } 18 19 private static void method3(List<String> list) { 20 // E set(int index,E element) 修改指定索引处的元素,返回被修改的元素 21 //被替换的那个元素,在集合中就不存在了. 22 String result = list.set(0, "qqq"); 23 System.out.println(result); 24 System.out.println(list); 25 } 26 27 private static void method2(List<String> list) { 28 // E remove(int index) 删除指定索引处的元素,返回被删除的元素 29 //在List集合中有两个删除的方法 30 //第一个 删除指定的元素,返回值表示当前元素是否删除成功 31 //第二个 删除指定索引的元素,返回值表示实际删除的元素 32 String s = list.remove(0); 33 System.out.println(s); 34 System.out.println(list); 35 } 36 37 private static void method1(List<String> list) { 38 // void add(int index,E element) 在此集合中的指定位置插入指定的元素 39 //原来位置上的元素往后挪一个索引. 40 list.add(0,"qqq"); 41 System.out.println(list); 42 } 43 }
3.数据结构
3.1数据结构之栈和队列【记忆】
栈结构
先进后出
队列结构
先进先出
3.2数据结构之数组和链表【记忆】
数组结构
查询快、增删慢
队列结构
查询慢、增删快
4.List集合的实现类
4.1List集合子类的特点【记忆】
ArrayList集合
底层是数组结构实现,查询快、增删慢
LinkedList集合
底层是链表结构实现,查询慢、增删快
4.2LinkedList集合的特有功能【应用】
特有方法
方法名 | 说明 |
---|---|
public void addFirst(E e) | 在该列表开头插入指定的元素 |
public void addLast(E e) | 将指定的元素追加到此列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从此列表中删除并返回第一个元素 |
public E removeLast() | 从此列表中删除并返回最后一个元素 |
示例代码
1 public class MyLinkedListDemo4 { 2 public static void main(String[] args) { 3 LinkedList<String> list = new LinkedList<>(); 4 list.add("aaa"); 5 list.add("bbb"); 6 list.add("ccc"); 7 // public void addFirst(E e) 在该列表开头插入指定的元素 8 //method1(list); 9 10 // public void addLast(E e) 将指定的元素追加到此列表的末尾 11 //method2(list); 12 13 // public E getFirst() 返回此列表中的第一个元素 14 // public E getLast() 返回此列表中的最后一个元素 15 //method3(list); 16 17 // public E removeFirst() 从此列表中删除并返回第一个元素 18 // public E removeLast() 从此列表中删除并返回最后一个元素 19 //method4(list); 20 21 } 22 23 private static void method4(LinkedList<String> list) { 24 String first = list.removeFirst(); 25 System.out.println(first); 26 27 String last = list.removeLast(); 28 System.out.println(last); 29 30 System.out.println(list); 31 } 32 33 private static void method3(LinkedList<String> list) { 34 String first = list.getFirst(); 35 String last = list.getLast(); 36 System.out.println(first); 37 System.out.println(last); 38 } 39 40 private static void method2(LinkedList<String> list) { 41 list.addLast("www"); 42 System.out.println(list); 43 } 44 45 private static void method1(LinkedList<String> list) { 46 list.addFirst("qqq"); 47 System.out.println(list); 48 } 49 } 50 51