集合框架的由来:数据多了用对象进行存储,对象多了用集合来进行存储。
迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为上像迭代器的东西都可以叫做迭代器。
迭代器是集合框架中特有的一种遍历方法
Iterator it = c.iterator();/*迭代器*/
这就是创建一个迭代器
迭代器的遍历:
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(22);
c.add(23);
c.add(26);
c.add(28);
c.add(65);
Iterator it = c.iterator();/*迭代器*/
while (it.hasNext()) {
System.out.println(it.next());
if(num % 2 == 0) {
System.out.println(num);
}
}
}
遍历以后得到如下结果:
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(22);
c.add(23);
c.add(26);
c.add(28);
c.add(66);//最后一个数如果改为奇数
Iterator it = c.iterator();/*迭代器*/
while (it.hasNext()) {
System.out.println(it.next());
if(num % 2 == 0) {
System.out.println(num);
}
}
}
则会出现下面这种结果:
这是因为迭代器遍历是指针向下选择最后一个值满足条件并且后面已经没有值了就会出现异常
集合的remov方法与迭代器的remov方法有什么区别?
1.在迭代器或者foreach循环删除的时候容易出现什么常见的问题
it.next()<容器中地区指针往下移>
Collection c=new ArrayList<>();
c.add(1);
c.add(2);
c.add(3);
c.add(4);
c.add(5);
//在迭代器或者foreach循环删除容易出现的问题
Iterator it = c.iterator();
while(it.hasNext()) {
int num=(int)it.next();
if(num % 2 == 0) {
System.out.println(it.next());//这里打印的是num的下一个数
c.remove(num);
//当在这里进行删除时,会导致java.util.ConcurrentModificationException错误
//多方对同一个元素进行操作 导致并发问题
}
}
2.在迭代器中执行collection.remove方法
java.util.ConcurrentModificationException<当前改变异常>
List的三个子类的特点:
Arraylist:
数组结构 增删慢,查询快 有连续下标 线程不同步 增长因子为1.5 10
Vector:
数组结构 增删改查都慢 有连续下标 线程同步 增长因子2 10
Vector相对ArrayList查询慢
Vector相对LinkedList增删慢
Linkedlist:
链表结构 增删快,查询慢 没有连续下标
Arraylist:数组结构 增删慢,查询快 有连续下标 线程不同步 增长因子为1.5 10
ArrayList al=new ArrayList<>(50);
for (int i = 0; i < 80; i++) {
al.add(i);
System.out.println(i+",");
getLen(al);
}
public static void getLen(ArrayList al) {
try {
Field f=al.getClass().getDeclaredField("elementData");
f.setAccessible(true);
Object obj=f.get(al);
Object [] elementData=(Object[]) obj;
System.out.println("底层数组的长度:"+elementData.length);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
集合框架Linkedlist:
public static void main(String[] args) {
Duizhan dz = new Duizhan();//堆栈
dz.push("a");
dz.push("b");
dz.push("c");
dz.push("d");
dz.push("e");
dz.bianli();
System.out.println("----");
dz.pop();
dz.bianli();
}
}
/**
* 堆栈结构的容器 特点 先进后出 最先进去的最后出来
*/
class Duizhan{
private LinkedList ll = new LinkedList<>();
public void push(Object obj) {
ll.addFirst(obj);
}
public Object pop() {
return ll.removeFirst();
}
/**
* 遍历
*/
public void bianli() {
Iterator it = ll.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
==ArrayListDemo ==
package com.azuo;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList al=new ArrayList();
// al.add("张三");
// al.add("李四");
// al.add("王五");
// al.add("李四");
// al.add("赵六");
//
al.add(new Person("张三",11));
al.add(new Person("李四",12));
al.add(new Person("王五",13));
al.add(new Person("李四",12));
al.add(new Person("赵六",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;
}
}