集合框架
1.Collection方法、迭代器
2.ArrayList特有方法、特有迭代器、具体对象特点、增长因子论证
3.LinkedList、链表的数据结构
4.ArrayList中的重复元素去重及其底层原理
Collection方法、迭代器
集合框架的由来:数据多了用对象进行存储,对象多了用集合来进行存储。
collection里所有的方法
代码片
下面展示一些 内联代码片。
ackage com.lzy; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /* * 讲解collection接口中的特别方法 * Iterator迭代器 */ /* * 集合的remove方法与迭代器的remove方法有什么区别 * 1.在迭代器或者foreach循环删除的时候容易出现什么常见的问题----it.next( * 2.在迭代器中执行collection.remove方法 * java.util.ConcurrentModificationException * 本质上是一个并发问题(两个对象操控一个) */
讯享网
讯享网public class CollectionDemo {
public static void main(String[] args) {
Collection c=new ArrayList(); c.add(11); c.add(22); c.add(33); c.add(44); c.add(55); // for (Object ob : c) {
// System.out.println(ob); // } //迭代器是集合所特有的遍历方式 //迭代器就相当于是娃娃机里的夹子 //迭代器通常都位于最顶端 //it.next()迭代器从最上面开始,取下一个 /*Iterator it=c.iterator(); while(it.hasNext()) { //System.out.println(it.next()); int num=(int)it.next(); if(num%2==0) { System.out.println(it.next()); //System.out.println(num); } }*/ Iterator it=c.iterator(); while(it.hasNext()) {
//System.out.println(it.next()); int num=(int)it.next(); if(num%2==0) {
System.out.println(it.next()); //System.out.println(num); //it.remove(); //c.remove(num); } } } }
代码效果展示图如下


ArrayList特有方法、特有迭代器、具体对象特点、增长因子论证
可以看到这类的容器是有下标,可以按照下标去取、删除…等等的方式去操作容器的元素
1.list集合相对于collection集合所特有的方法
2.Iterator.remove()与Collection.remove()的区别
3.Iterator与ListIterator的区别
public class ListDemo {
public static void main(String[] args) {
List c=new ArrayList(); c.add(22); c.add(23); c.add(26); c.add(27); c.add(29); c.add(30); // Iterator it=c.iterator(); // while(it.hasNext()) {
// System.out.println(it.next()); // } ListIterator it= c.listIterator(); while(it.hasNext()) {
System.out.println(it.next()); } while(it.hasPrevious()) {
System.out.println(it.previous()); } } }
代码效果展示图如下


增长因子论证
Arraylist如何进行性能调优? 为什么list集合底层是数据结构,但是数据长度优势固定的,而list长度又可以变?
代码片
下面展示一些 内联代码片。
(下面代码写到博客会出现白板,已截图代替)



LinkedList、链表的数据结构
特有方法
讯享网addFirst(); addLast();
代码片
下面展示一些 内联代码片。
(下面代码写到博客会出现白板,已截图代替)


代码效果展示图如下
public Object pop() {
return ll.getFirst();//一直都在,会一直是同一个,没有真的拿掉 }

代码效果展示图如下
讯享网 public Object pop() {
return ll.removeFirst(); }


ArrayList中的重复元素去重及其底层原理
判断list 集合中元素是否相同,依据的是元素的equals方法。
代码片
下面展示一些 内联代码片。
package com.lzy; / * 对ArrayList中的元素去重 *1.元素是字符串 *2.元素是自定义对象 * *集合collection的contains在调用的时候 *容器底层调用容器元素对象的equals方法 *之前元素对象是String *元素对象是Object(Person) */
讯享网public class ArrayListDemo {
public static void main(String[] args) {
ArrayList al=new ArrayList(); // al.add("huahua"); // al.add("yanyan"); // al.add("wanwan"); // al.add("yanyan"); // al.add("shishi"); al.add(new Person("huahua",11)); al.add(new Person("yanyan",12)); al.add(new Person("wanwan",13)); al.add(new Person("yanyan",12)); al.add(new Person("shishi",14)); ArrayList newal=repeat(al); System.out.println(newal.size()); } / * ArrayList容器中有重复的元素 * 1.建立一个新的容器 * 2.将老的容器遍历,并取出其中的元素 * 3.如果说这个元素存在于新的容器中,就不再往新的容器里添加, * 如果不存在,就加进去 */ public static ArrayList repeat(ArrayList al) {
ArrayList newal=new ArrayList<>(); for (Object object : al) {
if(!newal.contains(object)) {
newal.add(object); } } return newal; } } class Person{
private String name; private int age; public String getName() {
return name; } public void setName(String name) {
this.name = name; } public int getAge() {
return age; } public void setAge(int age) {
this.age = age; } @Override public String toString() {
return "Person [name=" + name + ", age=" + age + "]"; } public Person(String name, int age) {
super(); this.name = name; this.age = age; } public Person() {
super(); } @Override public boolean equals(Object obj) {
Person p=(Person)obj; //System.out.println(p.name+"--equals--"+this.name); return p.name.equals(this.name) && p.age==this.age; } }
代码效果展示图如下



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/56523.html