1 public class Student 2 { 3 // 成员变量 4 private String name; 5 private int age; 6 7 // 构造方法 8 public Student() 9 { 10 super(); 11 } 12 13 public Student(String name, int age) 14 { 15 super(); 16 this.name = name; 17 this.age = age; 18 } 19 20 // 成员方法 21 // getXxx()/setXxx() 22 public String getName() 23 { 24 return name; 25 } 26 27 public void setName(String name) 28 { 29 this.name = name; 30 } 31 32 public int getAge() 33 { 34 return age; 35 } 36 37 public void setAge(int age) 38 { 39 this.age = age; 40 } 41 42 @Override 43 public String toString() 44 { 45 return "Student [name=" + name + ", age=" + age + "]"; 46 } 47 }
1 /** 2 把5个学生的信息存储到数组中,并遍历数组,获取获得每个学生信息。 3 * 学生:Student 4 * 成员变量:name,age 5 * 构造方法:无参,带参 6 * 成员方法:getXxx()/setXxx() 7 * 分析: 8 * A:建立学生类。 9 * B:建立学生数组(对象数组)。 10 * C:建立5个学生对象,并赋值。 11 * D:把C步骤的元素,放到数组中。 12 * E:遍历学生数组。 13 * */ 14 15 public class Practice 16 { 17 public static void main(String[] args) 18 { 19 // 建立学生数组(对象数组)。 20 Student[] students = new Student[5]; 21 // for (int x = 0; x < students.length; x++) 22 // { 23 // System.out.println(students[x]); 24 // } 25 // System.out.println("---------------------"); 26 27 // 建立5个学生对象,并赋值。 28 Student s1 = new Student("小明", 27); 29 Student s2 = new Student("小红", 30); 30 Student s3 = new Student("小强", 30); 31 Student s4 = new Student("旺财", 12); 32 Student s5 = new Student("张三", 35); 33 34 // 将对象放到数组中。 35 students[0] = s1; 36 students[1] = s2; 37 students[2] = s3; 38 students[3] = s4; 39 students[4] = s5; 40 41 // 遍历 42 for (int x = 0; x < students.length; x++) 43 { 44 //System.out.println(students[x]); 45 Student s = students[x]; 46 System.out.println(s.getName()+"---"+s.getAge()); 47 } 48 } 49 }
集合类的由来:面向对象语言对事物的体现都是以对象的形式,因此为了方便对多个对象的操做,Java就提供了集合类。java
数组能够存储同一种类型的基本数据也能够存储同一种类型的对象,但长度是固定的android
集合类的特色:集合只用于存储对象,集合长度是可变的,集合能够存储不一样类型的对象。swift
集合容器由于内部的数据结构不一样,有多种具体容器,根据共性内容不断的向上抽取,就造成了集合框架。数组
Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 容许有重复的元素,而另外一些则不容许。一些 collection 是有序的,而另外一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List)实现。此接口一般用来传递 collection,并在须要最大广泛性的地方操做这些 collection。安全
2. boolean remove(Objecto):今后 collection 中移除指定元素的单个实例,若是存在的话(可选操做)。markdown
4. boolean contains(Objecto):若是此 collection 包含指定的元素,则返回 true。数据结构
1 // 建立集合对象 2 // Collection c = new Collection(); //错误,由于接口不能实例化 3 Collection c = new ArrayList(); 4 c.add("hello"); 5 c.add("world"); 6 c.add("java"); 7 // c.clear();//移除全部元素 8 // System.out.println("remove:" + c.remove("hello"));//移除一个元素 9 // System.out.println("remove:" + c.remove("javaee")); 10 // 判断集合中是否包含指定的元素 11 System.out.println("contains:"+c.contains("hello"));//contains:true 12 System.out.println("contains:"+c.contains("android"));//contains:false 13 //判断集合是否为空 14 System.out.println("isEmpty:"+c.isEmpty());//isEmpty:false 15 //元素的个数 16 System.out.println("size:"+c.size());//size:3 17 System.out.println("c:" + c);//c:[hello, world, java]
移除此 collection 中那些也包含在指定 collection 中的全部元素(可选操做)。并发
仅保留此 collection 中那些也包含在指定 collection 的元素(可选操做)。换句话说,移除此 collection 中未包含在指定 collection 中的全部元素。框架
c1.addAll(c2);//将c2集合中的全部元素添加到c1集合中,c1变c2不变 c1.removeAll(c2);//将c1集合中与c2集合相同的全部元素删除,只要有一个相同的就返回true c1.containsAll(c2);//判断c1集合中的元素是否包含c2中的所有元素,所有包含则返回true c1.retainAll(c2);//将c1集合中与c2集合相同的元素保留,删除其余元素,返回值表示c1集合是否发生变化,发生变化返回true,没有变化返回false
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 建立集合 6 Collection c = new ArrayList(); 7 c.add("hello"); 8 c.add("world"); 9 c.add("java"); 10 11 Object[] objs = c.toArray(); 12 for (int i = 0; i < objs.length; i++) 13 { 14 //向下转为String类型 15 String s = (String)objs[i]; 16 System.out.println(s+":"+s.length()); 17 } 18 } 19 }
hello:5 world:5 java:4
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 建立集合 6 Collection c = new ArrayList(); 7 //建立学生对象并添加到集合 8 c.add(new Student("小明",23)); 9 c.add(new Student("小红",32)); 10 c.add(new Student("小强",14)); 11 c.add(new Student("旺财",8)); 12 c.add(new Student("张三",16)); 13 14 Object[] objs = c.toArray(); 15 for (int i = 0; i < objs.length; i++) 16 { 17 Student s = (Student)objs[i]; 18 System.out.println(s.getName()+":"+s.getAge()); 19 } 20 } 21 }
小明:23 小红:32 小强:14 旺财:8 张三:16
1 // 建立集合 2 Collection c = new ArrayList(); 3 //建立元素并添加到集合 4 c.add("hello"); 5 c.add("world"); 6 c.add("java"); 7 //获取迭代器,实际返回的是子类对象,多态 8 Iterator it = c.iterator(); 9 while(it.hasNext()) 10 { 11 System.out.println(it.next()); 12 }
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 建立集合 6 Collection c = new ArrayList(); 7 //建立学生对象并添加到集合 8 c.add(new Student("小明",23)); 9 c.add(new Student("小红",32)); 10 c.add(new Student("小强",14)); 11 c.add(new Student("旺财",8)); 12 c.add(new Student("张三",16)); 13 14 Iterator it = c.iterator(); 15 while(it.hasNext()) 16 { 17 Student s = (Student)it.next(); 18 System.out.println(s.getName()+":"+s.getAge()); 19 } 20 } 21 }
Iterator it = c.iterator(); while(it.hasNext()) { Student s = (Student)it.next(); System.out.println(s.getName()+":"+s.getAge()); }
for(Iterator it = c.iterator();it.hasNext();) { Student s = (Student)it.next(); System.out.println(s.getName()+":"+s.getAge()); }
Iterator it = c.iterator();
while(it.hasNext()) { System.out.println(((Student)it.next()).getName()); System.out.println(((Student)it.next()).getAge()); }
上面的代码表示获取的是第1个学生的姓名,第2个学生的年龄,以此类推,若是集合中的元素是奇数个,则会报NoSuchElementException错误less
假设迭代器定义的是一个类,那么就能够建立该类的对象,调用该类的方法来实现集合的遍历,可是Java中提供了不少的集合类,而这些集合类的数据结构是不一样的,因此存储的方式和遍历的方式也应该是不一样的,进而它们的遍历方式也应该是不同的。最终就没有定义迭代器类
而不管使用哪一种集合都应该具有获取元素的操做,而且最好再辅助与判断功能,也就是说判断功能和获取功能应该是一个集合遍历所具有的,而每种集合的方式又不太同样,因此将这两个功能给提取出来,并不提供具体实现,这种方式就是接口,而真正具体的实现类在具体的子类中之内部类的方式体现的
public interface Iterator { boolean hasNext(); Object next(); } public interface Iterable { Iterator iterator(); } public interface Collection extends Iterable { Iterator iterator(); } public interface List extends Collection { Iterator iterator(); } public class ArrayList implements List { public Iterator iterator() { return new Itr(); } //内部类 private class Itr implements Iterator { public boolean hasNext() {} public Object next(){} } } Collection c = new ArrayList(); c.add("hello"); c.add("world"); c.add("java"); Iterator it = c.iterator(); //new Itr(); while(it.hasNext()) { String s = (String)it.next(); System.out.println(s); }
1 import java.util.ArrayList; 2 import java.util.Collection; 3 import java.util.Iterator; 4 5 public class Practice 6 { 7 8 public static void main(String[] args) 9 { 10 11 // 建立集合 12 Collection c = new ArrayList(); 13 14 //添加字符串 15 c.add("hello"); 16 c.add("你好"); 17 c.add("world"); 18 c.add("java"); 19 c.add("旺财"); 20 //经过集合对象获取迭代器对象 21 22 Iterator it = c.iterator(); 23 while(it.hasNext()) 24 25 { 26 String s = (String)it.next(); 27 28 System.out.println(s); 29 } 30 31 } 32 33 }
1 import java.util.ArrayList; 2 import java.util.Collection; 3 import java.util.Iterator; 4 5 public class Practice 6 { 7 public static void main(String[] args) 8 { 9 // 建立集合 10 Collection c = new ArrayList(); 11 //建立学生对象并添加到集合 12 c.add(new Student("小明",23)); 13 c.add(new Student("小红",32)); 14 c.add(new Student("小强",14)); 15 c.add(new Student("旺财",8)); 16 c.add(new Student("张三",16)); 17 18 Iterator it = c.iterator(); 19 while(it.hasNext()) 20 { 21 Student s = (Student)it.next(); 22 System.out.println(s.getName()+":"+s.getAge()); 23 } 24 } 25 }
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 建立集合 6 List list = new ArrayList(); 7 list.add("hello"); 8 list.add("world"); 9 list.add("java"); 10 11 Iterator it = list.iterator(); 12 while(it.hasNext()) 13 { 14 String s = (String)it.next(); 15 System.out.println(s); 16 } 17 } 18 }
List接口概述:有序的(存取顺序一致)collection(也称为序列)。此接口的用户能够对列表中每一个元素的插入位置进行精确地控制。用户能够根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 建立集合 6 List list = new ArrayList(); 7 //建立学生对象并添加到集合 8 list.add(new Student("小明",23)); 9 list.add(new Student("小红",32)); 10 list.add(new Student("小强",14)); 11 list.add(new Student("旺财",8)); 12 list.add(new Student("张三",16)); 13 14 Iterator it = list.iterator(); 15 while(it.hasNext()) 16 { 17 Student s = (Student)it.next(); 18 System.out.println(s.getName()+":"+s.getAge()); 19 } 20 } 21 }
list.remove(1)//删除集合中1位置上的元素,返回被删除的元素,改变集合长度
list.set(2, "javaee")//将集合中2位置上的元素替换为javaee,返回被替换的元素,不改变集合长度
1 for (int i = 0; i < list.size(); i++) 2 3 { 4 5 String s = (String)list.get(i); 6 7 System.out.println(s); 8 9 }
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 建立集合 6 List list = new ArrayList(); 7 //建立学生对象并添加到集合 8 list.add(new Student("小明",23)); 9 list.add(new Student("小红",32)); 10 list.add(new Student("小强",14)); 11 list.add(new Student("旺财",8)); 12 list.add(new Student("张三",16)); 13 14 for (int i = 0; i < list.size(); i++) 15 { 16 Student s =(Student)list.get(i); 17 System.out.println(s.getName()+":"+s.getAge()); 18 } 19 } 20 }
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 建立集合 6 List list = new ArrayList(); 7 8 list.add("hello"); 9 list.add("world"); 10 list.add("java"); 11 //列表迭代器 12 ListIterator lit = list.listIterator(); 13 //正向遍历 14 while(lit.hasNext()) 15 { 16 String s = (String)lit.next(); 17 System.out.println(s); 18 } 19 System.out.println("-----"); 20 //逆向遍历 21 while(lit.hasPrevious()) 22 { 23 //获取上一个元素 24 String s = (String)lit.previous(); 25 System.out.println(s); 26 } 27 28 } 29 }
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 建立集合 6 List list = new ArrayList(); 7 8 list.add("hello"); 9 list.add("world"); 10 list.add("java"); 11 Iterator it = list.iterator(); 12 while(it.hasNext()) 13 { 14 String s = (String)it.next(); 15 if(s.equals("world")) 16 list.add("javaee"); 17 } 18 System.out.println(list); 19 } 20 }
错误产生缘由:迭代器是依赖于集合存在的,在迭代的过程当中使用集合的方法添加元素迭代器是不知道的,因此报错,并发修改异常
解决方案:1.用迭代器迭代元素时使用迭代器修改元素(ListIterator列表迭代器),添加的元素在迭代的元素后面
ArrayList:底层数据结构是数组,查询快,增删慢,是不一样步的,线程不安全,效率高
LinkedList:底层数据结构是链表,查询慢,增删快,是不一样步的,线程不安全,效率高