Collection 和 Iterator

来源:互联网 发布:添加数组中的指定元素 编辑:程序博客网 时间:2024/06/06 14:13

Collection 和 Iterator

代表的是顶层次的一个根接口! 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 iterator():集合的专有迭代遍历方式

交集功能:
boolean retainAll(Collection c):对一个集合取交集(思考:返回值类型表达的是什么意思?交集的元素去哪里?)

集合的转换功能:
Object[] toArray():将集合转换数组

代码:

public static void main(String[] args) {    //创建集合      Collection  c = new ArrayList();      Collection  c2= new ArrayList();      //添加      c.add("hello");      c.add("world");      c.add("hello");      c.add("java");      c2.add("hello");      c2.add("love");      System.out.println(c);//底部已经重写了toString()方法      //删除功能    /*  和删除功能相关的方法:          *         boolean remove(Object o):删除指定的元素          *         boolean removeAll(Collection c):删除一个集合中的元素(思考:删除一个元素算是删除呢还是删除所有算是删除?)          *         void clear():删除一个集合中的所有元素          */      System.out.println(c.remove("hello"));//只要删除了就返回true      System.out.println(c);      System.out.println(c.removeAll(c2));      System.out.println(c);      /*       * 和判断功能相关的方法:

* boolean contains(Object o):判断该集合中是否包含指定的元素
* boolean containsAll(Collection c):判断是否包含一个集合中的元素(包含一个算是包含,还是包含所有元素算术包含?)
* boolean isEmpty():判断集合是否为空
* */
System.out.println(c.isEmpty());
System.out.println(c.containsAll(c2));//包含全部算包含

      /*       * 长度功能:      int size()       * */      System.out.println(c.size());      /*       * 交集功能:

* boolean retainAll(Collection c):对一个集合取交集(思考:返回值类型表达的是什么意思?交集的元素去哪里?)
* */
System.out.println(c);
System.out.println(c2);
System.out.println(c.retainAll(c2));//交集元素存在c集合中,boolean表达的意思是c集合中的元素有没有发生变化
System.out.println(c);
}

结果

[hello, world, hello, java]
true
[world, hello, java]
true
[world, java]
false
false
2
[world, java]
[hello, love]
true
[]

需求;
Obejct[] toArray():把集合转换成数组

代码:
public static void main(String[] args) {
Collection c = new ArrayList();

    c.add("hello");    c.add("mu");    c.add("yang");    Object[]  obj=  c.toArray();    System.out.println(obj);//[Ljava.lang.Object;@79f1d448    //遍历    for (int x =0;x<obj.length;x++){        String s = (String)obj[x];        System.out.println(s);    }}

结果:

[Ljava.lang.Object;@31731334
hello
mu
yang

需求:
给集合中存储4个学生对象,最终要把5个学生的信息遍历出来!

代码:
public static void main(String[] args) {
Collection c= new ArrayList();

    sdudent s1 = new sdudent("张三",20,"188");    sdudent s2 = new sdudent("李四",21,"186");    sdudent s3 = new sdudent("王五",22,"185");    c.add(s1);    c.add(s2);    c.add(s3);    Iterator i =  c.iterator();    for(int x =0;x<c.size();x++){        sdudent s = (sdudent) i.next();        System.out.println(s.getName()+"-"+s.getAge()+"-"+s.getWeight());    }}

结果

张三-20-188
李四-21-186
王五-22-185