Java自学-集合(2)
1、集合体系结构
2、Collection集合概述和使用
package lesson01;
import java.util.ArrayList;
import java.util.Collection;
/**
* Author: Gu Jiakai
* Date: 2021/8/13 6:46
* FileName: CollectionDemo01
* Description:
*/
public class CollectionDemo01 {
public static void main(String[] args) {
Collection<String> c=new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
System.out.println(c);
}
}
3、Collection集合常用方法
鼠标中键或者选中想要查看的类,Ctrl+B。
Alt+7:打开一个窗口,可以看到类的所有信息。
package lesson01;
import java.util.ArrayList;
import java.util.Collection;
/**
* Author: Gu Jiakai
* Date: 2021/8/13 21:41
* FileName: CollectionDemo02
* Description:
*/
public class CollectionDemo02 {
public static void main(String[] args) {
Collection<String> c=new ArrayList<String>();
// System.out.println(c.add("hello"));
// System.out.println(c.add("world!"));
// System.out.println(c.add("world!"));
c.add("hello");
c.add("world");
c.add("java");
// System.out.println(c.remove("world"));
// System.out.println(c.remove("javaee"));
// c.clear();
// System.out.println(c.contains("world"));
// System.out.println(c.contains("javaee"));
// System.out.println(c.isEmpty());
System.out.println(c.size());
System.out.println(c);
}
}
4、Collection集合的遍历
package lesson01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Author: Gu Jiakai
* Date: 2021/8/13 21:55
* FileName: IteratorDemo
* Description:
*/
public class IteratorDemo {
public static void main(String[] args) {
Collection<String> c=new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
Iterator<String> iterator = c.iterator();
while(iterator.hasNext()){
// System.out.println(iterator.next());
String next = iterator.next();
System.out.println(next);
}
}
}