容器(collection)初步

容器(集合)的分类:

泛型(generic):本质是数据类型的参数化(提前告诉编译器,在调用泛型时必须传入实际类型)

例:E即为在主函数中定义的传入的实际类型

class MyCollection<E>{
Object[] objs = new Object[5];

public void Set(E e,int a) {
objs[a] = e;
}

public E Get(int a) {
return (E)(objs[a]);
}
}

Collectio接口方法:

Collection <String> c = new ArrayList<>();

c.size():输出c的大小

c.isEmpty():验证c是否为空,返回值为true or false

c.add("l"):向c中插入对象l

c.contains("l"):验证c中是否包含l,返回值为true or false

c.remove("l"):将l对象从c中移除(l对象仍存在,但地址不再与c关联)

c.clean():清除c中所有对象

集合与集合之间的方法:

List<String> list01 = new ArrayList<String>();
List<String> list02 = new ArrayList<String>();

  list01.removeAll(list02):移除01中与02重合的对象

  list01.addAll(list02):添加02中所有的项到01

  list01.retainAll(list02):仅保留01、02重合的对象

  list01.containsAll(list02):验证01是否完全包含02,返回值为true or false。

list方法:

list.add(1,"ljl"):add方法的重载,在1处添加对象

list.set(1, "xrw"):将1处的对象更改为xrw

list.remove(4):移除4处的对象

list.get(1):get1处的对象

list.indexOf("A"):返回第一个A所处位置的索引,如果没有则返回-1

list.lastIndexOf("A"):返回最后一个A所处索引的位置,如果没有则返回-1

猜你喜欢

转载自www.cnblogs.com/LuJunlong/p/11568099.html