数组:存储的是同一种数据类型的元素:水杯中的水
基本类型:共八种 四种整形
byte 一字节
short 二字节
int 四字节
long 8字节
两种浮点类型
fioat 4字节
double 8字节
一种字符单元
char 2字节
一种真值类型
public static void main(String[] args) { Collection c = new ArrayList(); Student s1 = new Student(22, "gao"); Student s2 = new Student(11,"wei"); c.add(s1); c.add(s2); System.out.println(c);//输出[Student [age=22, name=gao], Student [age=11, name=wei]] //遍历集合 需要将集合转换成数组 Object[] o = c.toArray(); Student s = (Student) o[1];//输出Student [age=11, name=wei] System.out.println(s); }
2. Collection的添加功能: boolean add(E e):添加功能 获取功能: Object[] toArray();返回包含此 collection 中所有元素的数组。 public static void main(String[] args) { Collection c = new ArrayList(); Student s1 = new Student(22, "gao"); Student s2 = new Student(11,"wei"); c.add(s1); c.add(s2); System.out.println(c);//输出[Student [age=22, name=gao], Student [age=11, name=wei]] //遍历集合 需要将集合转换成数组 Object[] o = c.toArray(); Student s = (Student) o[1];//输出Student [age=11, name=wei] System.out.println(s); } public static void main(String[] args) { Collection c = new ArrayList(); Student s1 = new Student(22, "gao"); Student s2 = new Student(11,"wei"); c.add(s1); c.add(s2); System.out.println(c);//输出[Student [age=22, name=gao], Student [age=11, name=wei]] //遍历集合 需要将集合转换成数组 Object[] o = c.toArray(); Student s = (Student) o[1];//输出Student [age=11, name=wei] System.out.println(s); } 3. Collection:代表的是顶层次的一个根接口! jdk不提供此接口的任何直接实现,而是子接口的具体类进行实现! 和添加功能相关的方法: boolean add(Object obj):添加一个元素 boolean addAll(Collection c):添加一个集合中的元素 和删除功能相关的方法: boolean remove(Object o):删除指定的元素 boolean removeAll(Collection c):删除一个集合中的元素(删除一个元素) void clear():删除一个集合中的所有元素 和判断功能相关的方法: boolean contains(Object o):判断该集合中是否包含指定的元素 boolean containsAll(Collection c):判断是否包含一个集合中的元素(包含所有元素算包含) boolean isEmpty():判断集合是否为空 长度功能: int size() 面试题:数组中有没有length()方法?String类有没有length()方法?集合中有没有length()方法? 数组:length属性 字符串String:length() 集合:size() Iterator<E> iterator():集合的专有迭代遍历方式 交集功能: boolean retainAll(Collection c):对一个集合取交集(思考:返回值类型表达的是什么意思?交集的元素去哪里?) 集合的转换功能: Object[] toArray():将集合转换数组 4. 迭代器 4.1 迭代器原理 interface Iterator{ boolean hasNext() ; Object next(); } public interface Iterable { Iterator iterator(); } public interface Collection extends Iterable{ public Iterator<E> iterator() { return new Itr(); //Itr:类 } //具体的类 :内部类的另一种形式 private class Itr implements Iterator { //Itr是Iterator接口的子实现类 public boolean hasNext() {//重写了hasNext() return cursor != size; } public Object next() { } } } collection c = new ArrayList(); c.add("hello"); c.add("world"); c.add("java"); Iterator it = c.iterator();//本应该是Iterator it = new Itr() ;而 c.iterator()返回值是new Itr()//接口多态(开发中经常用) while(it.hasNext){ String s = (String)it.next(); System.out.println(s); } 5 List是Collection的一个子接口,ArrayList是List的实现类 List集合是一个有序的集合,是可以有重复的元素 有序:指的是元素存储和取出是一致的!(存储的是什么,取出的就是什么) Set集合:是另一个子接口:元素不能重复,无序 5.2 List集合的特有功能: 添加功能: void add(int index,Object element):在指定位置添加指定的元素 获取功能: Object get(int index):获取指定位置的元素 删除功能: Object remove(int index):删除指定位置的元素,返回的就是删除的元素 5.3 使用三种方法遍历List集合 public static void main(String[] args) { List l = new ArrayList(); l.add("hello"); l.add("ha"); l.add("fenghui"); Iterator i = l.iterator(); while(i.hasNext()){ String s = (String) i.next(); System.out.println(s); } for(int x = 0; x<l.size(); x++){ Object s1 = l.get(x); System.out.println(s1); } ListIterator ii = l.listIterator(); while(ii.hasNext()){ Object s2 = ii.next(); System.out.println(s2);; } } } 5.4 需求:给List存储字符串,判断如果这个字符串是"world",然后给集合中添加一个新的 字符串"java //1)两种解决并发修改异常的方式: //使用迭代器遍历,迭代器添加,Iterator接口没有添加功能,所有使用列表迭代器:ListIterator //获取列表迭代器 ListIterator lit = list.listIterator() ; while(lit.hasNext()){ String s = (String) lit.next() ; //判断,迭代器添加 if(s.equals("world")){ //列表迭代器的特有功能 lit.add("javaee");//插入元素:在指定元素插入新的元素 } } System.out.println("list:"+list); //2)使用集合遍历,使用集合添加 for(int x = 0 ; x < list.size() ; x ++){ String s = (String) list.get(x) ; //判断有world,采用集合添加,在末尾添加 if(s.equals("world")){ list.add("javaee") ; } } System.out.println("list:"+list); } }
