Collection:是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。
Collection接口使用所有单列集合的根接口,所有单列集合的父类
public boolean add(E e) 添加元素 boolean addAll(Collection c):添加一个集合的元素 public boolean remove(Object obj) 删除元素obj 删除成功返回true boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有) public void clear() 清空集合 public int size() 获得集合元素个数 public boolean contains(Object o) 判断集合中是否包含指定的元素o boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有) boolean retainAll(Collection c):两个集合都有的元素 Object[] toArray() 把集合转换为数组 public boolean isEmpty() 判断集合是否空 Iterator<E> iterator() 获取迭代器
// 创建集合 Collection<String> c = new ArrayList<>(); // 添加元素 c.add("aaa"); c.add("bbb"); c.add("ccc"); System.out.println(c); // 删除元素 c.remove("ccc"); System.out.println(c.size()); // 判断集合中是否包含指定的元素 System.out.println(c.contains("eee")); System.out.println(c.isEmpty());// false // 清空集合 c.clear(); System.out.println(c.isEmpty());// true System.out.println(c);
// 创建集合对象 // Collection c = new Collection(); //错误,因为接口不能实例化 Collection c = new ArrayList(); // boolean add(Object obj):添加一个元素 System.out.println("add:"+c.add("hello")); c.add("hello"); c.add("world"); c.add("java"); // boolean remove(Object o):移除一个元素 System.out.println("remove:" + c.remove("hello")); System.out.println("remove:" + c.remove("javaee")); // boolean contains(Object o):判断集合中是否包含指定的元素 System.out.println("contains:"+c.contains("hello")); System.out.println("contains:"+c.contains("android")); // boolean isEmpty():判断集合是否为空 System.out.println("isEmpty:"+c.isEmpty()); // void clear():移除所有元素 c.clear(); // int size():元素的个数 System.out.println("size:" + c.size()); c.add("java"); System.out.println("c:" + c);
// 创建集合1 Collection c1 = new ArrayList(); c1.add("abc1"); c1.add("abc2"); c1.add("abc3"); c1.add("abc4"); // 创建集合2 Collection c2 = new ArrayList(); c2.add("abc5"); c2.add("abc6"); c2.add("abc7"); // boolean addAll(Collection c):添加一个集合的元素 System.out.println("addAll:" + c1.addAll(c2)); // boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有) // 只要有一个元素被移除了,就返回true。 System.out.println("removeAll:"+c1.removeAll(c2)); c1.addAll(c2); c2.add("abc00"); System.out.println("c1:" + c1); // boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有) // 只有包含所有的元素,才叫包含 System.out.println("containsAll:"+c1.containsAll(c2)); // boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了, //返回的boolean又是什么意思呢? // 假设有两个集合A,B。 // A对B做交集,最终的结果保存在A中,B不变。 // 返回值表示的是A是否发生过改变。 System.out.println("retainAll:" + c1.retainAll(c2)); System.out.println("c1:" + c1); System.out.println("c2:" + c2);
集合的遍历。其实就是依次获取集合中的每一个元素。
Object[] toArray():把集合转成数组,可以实现集合的遍历
/* * 集合的遍历。其实就是依次获取集合中的每一个元素。 * * Object[] toArray():把集合转成数组,可以实现集合的遍历 */ public static void main(String[] args) { // 创建集合对象 Collection c = new ArrayList(); // 添加元素 c.add("hello"); // Object obj = "hello"; 向上转型 c.add("world"); c.add("java"); // 遍历 // Object[] toArray():把集合转成数组,可以实现集合的遍历 Object[] objs = c.toArray(); for (int x = 0; x < objs.length; x++) { // 我知道元素是字符串,我在获取到元素的的同时,还想知道元素的长度。 // 上面的实现不了,原因是Object中没有length()方法 // 我们要想使用字符串的方法,就必须把元素还原成字符串 // 向下转型 String s = (String) objs[x]; System.out.println(s + "---" + s.length()); } System.out.println("-----------------------------"); for(Object o:c){ String s = (String) o; System.out.println(s + "---" + s.length()); }
注意事项
如果遍历的是引用数据类型对象,则for循环内使用引用变量修改对象的成员变量值会影响集合或数组中对象的成员变量值。
什么叫数据结构:
数据的存储方式。