目录
编辑List
Set
集合排序问题
queue
迭代器iterator
扩展题:
List
有序(自然顺序——元素存入的先后顺序)、可重复
1.ArrayList
- - 底层Object类型的数组
- - 线程非安全
- - 默认初始化容量:10
- - 扩容算法:1.5倍
- - subList:截取字串,但和原串共用一个资源
- - 做查询快
- - subList:截取字串,但和原串共用一个资源
- - 扩容算法:1.5倍
- - 默认初始化容量:10
- - 线程非安全
- ①默认初始容量:10
- ②扩容机制
- ③初始化过程
2.LinkedList
- - 底层是(动态)链表实现,内部封装一个静态内部类Node
- - 做修改的时候快,因为是指针实现的
3.Vector
- 和ArrayList实现原理一致,但是线程安全,效率低,一般不用
4.Stack
- 先进后出
四种实现的方法都差不多,但是底层原理不一样
//(1)初始化过程 ArrayList() ArrayList(int i)
ArrayList list1 = new ArrayList<>();//初始化了一个无长度的空数组
//size()指的是元素数量,不是容量大小
System.out.println(list1.size());//0
ArrayList list2 = new ArrayList<>(15);//初始化了一个指定长度的数组
System.out.println(list2.size());//0
//(2)扩容机制
list1.add("java");
list1.add("c");
list1.add("c++");
list1.add("python");
list1.add("php");
//subList:截取字串 字串和原串共用一块资源
List sublist = list1.subList(1, 4);//前包后不包
System.out.println(sublist);//[c, c++, python]
//
for (int i = 0; i < sublist.size(); i++) {
sublist.set(i,sublist.get(i)+1);
}
System.out.println(sublist);//[c1, c++1, python1]
System.out.println(list1);//[java, c1, c++1, python1]
Stackstack = new Stack<>(); stack.push(1); stack.push(2); stack.push(3); stack.push(4); System.out.println(stack);//[1, 2, 3, 4] System.out.println(stack.pop());//4 System.out.println(stack);//[1, 2, 3] System.out.println(stack.peek());//3 System.out.println(stack);//[1, 2, 3] System.out.println(stack.empty());//false
Set
无序、无重复
- 1.HashSet:底层-->HashMap
- 2.LinkedHashSet:让无序的HashSet变有序(自然顺序)
- 3.TreeSet:排序
//1.测试Set的特征 HashSetset = new HashSet<>(); set.add("java"); set.add("c"); set.add("php"); set.add("c++"); set.add("python"); set.add("python");//自动消除重复的元素 System.out.println(set);//[c++, python, java, c, php] //2.测试LinkedHashSet LinkedHashSet linkedHashSet = new LinkedHashSet<>(); linkedHashSet.add("java"); linkedHashSet.add("c"); linkedHashSet.add("c++"); linkedHashSet.add("python"); linkedHashSet.add("php"); linkedHashSet.add("php"); System.out.println(linkedHashSet);//[java, c, c++, python, php] //TreeSet TreeSet treeSet = new TreeSet<>(); for (int i = 0; i < 10; i++) { treeSet.add((int)(Math.random()*100)); } System.out.println(treeSet);//[6, 23, 25, 36, 51, 55, 63, 77, 95, 97] //[5, 20, 24, 40, 71, 72, 80, 91, 93]——随机到相同数字时会自动消除
集合排序问题
- (一)集合排序的排序规则
- 1.如果要对对象进行排序,则该对象必须实现Comparable接口
- 2.重写compareTo方法设置排序的规则e
- 3.根据返回值确定大小 大于、等于、小于,所对应返回值整数为:正整数、零、负整数
- (二)集合的临时排序规则
- 1.在sort方法第二个方法中采用匿名内部类的形式设置临时比较规则
- (三)针对于List
//1.测试Integer的排序
ArrayList list = new ArrayList<>();
for (int i = 0; i < 7; i++) {
list.add((int)(Math.random()*100));
}
System.out.println("原集合:" + list);
Collections.sort(list);//排序
//依据什么排序?怎么改变规则?
System.out.println("排序后:"+list);
//2.测试对Person的salary排序
ArrayList persons = new ArrayList<>();
persons.add(new Person("张三",10000.32));
persons.add(new Person("张四",9000.32));
persons.add(new Person("张五",12000.22));
persons.add(new Person("张六",9900.12));
persons.add(new Person("张七",8900.52));
System.out.println("排序前:" + persons);
Collections.sort(persons);
System.out.println("排序后:" + persons);
//2.测试临时比较规则:此规则只在此次比较中有效
Collections.sort(persons, new Comparator() {
@Override
public int compare(Person o1, Person o2) {
// return o1.getSalary() < o2.getSalary() ? 1 : ((o1.getSalary() == o2.getSalary()) ? 0 : -1);
return o1.getSalary() < o2.getSalary() ? 1 : ((o1.getSalary().equals(o2.getSalary())) ? 0 : -1);
}
});
System.out.println("排序后:" + persons);
queue
- 1.如果要对对象进行排序,则该对象必须实现Comparable接口
- 2.重写compareTo方法设置排序的规则e
- 3.根据返回值确定大小 大于、等于、小于,所对应返回值整数为:正整数、零、负整数
- 2.重写compareTo方法设置排序的规则e
- 1.在sort方法第二个方法中采用匿名内部类的形式设置临时比较规则
一、队列
实现类常用LinkedList
先进先出
//入队 Queuequeue = new LinkedList<>(); queue.offer("a"); queue.offer("c"); queue.offer("v"); queue.offer("g"); System.out.println(queue);//[a, c, v, g] //出队 System.out.println(queue.poll());//a System.out.println(queue);//[c, v, g] //获取队首元素 System.out.println(queue.peek());//c System.out.println(queue);//[c, v, g]
二、双端队列(deque)
双端队列如果限定只能从一端进一端出,和栈无区别
Dequedeque = new LinkedList<>(); deque.offerFirst("a"); deque.offerFirst("b"); deque.offerFirst("c"); deque.offerLast("d"); deque.offerLast("e"); deque.offerLast("f"); System.out.println(deque);//[c, b, a, d, e, f] System.out.println(deque.pollFirst());//c System.out.println(deque.pollLast());//f System.out.println(deque);//[b, a, d, e]
迭代器iterator
专门用来遍历集合的
ArrayListlist = new ArrayList<>(); for (int i = 0; i < 7; i++) { list.add((int)(Math.random()*100)); } //1.集合的遍历 System.out.println("------基本for循环------"); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)+" "); } System.out.println(); System.out.println("------增强for循环------"); for (Integer integer : list) { System.out.print(integer+" "); } System.out.println(); System.out.println("------foreach方法------"); list.forEach(o -> System.out.print(o+" ")); // list.forEach(new Consumer () { // @Override // public void accept(Integer integer) { // System.out.print(integer+" "); // } // }); System.out.println(); System.out.println("------迭代器------"); //获取迭代器对象 Iterator iterator = list.iterator(); while (iterator.hasNext()) {//判断是否有下一个元素 (尾插法) //判断删除大于50的所有元素 if (iterator.next() > 50) { //ConcurrentModificationException 用迭代器遍历出的元素,必须使用迭代器的remove方法删除,而不能使用集合的方法 //list.remove(iterator.next()); iterator.remove(); } } System.out.print(list+" ");
扩展题:
增强for循环:原理是迭代器,而迭代器是专门用来遍历集合的,但数组不是集合,为什么增强for循环可以遍历数组?
答:通过查看反编译,使用增强for循环遍历数组时,会解析成一般for循环,但遍历集合时,会解析成迭代器



