轉自:
http://www.java265.com/JavaCourse/202204/2947.html
下文筆者將講述集合的簡介說明,如下所示
Collection集合簡介
Collection是集合的頂層介面 用于存盤一組物件
Collection中常用的方法
| 方法名 | 相關說明 |
| boolean add(E e) | 添加元素 |
| boolean remove(Object o) | 從集合中移除指定元素 |
| void clear() | 清空集合中的元素 |
| boolean contains(Object o) | 判斷集合中是否存在指定元素 |
| boolean isEmpty() | 判斷集合是否為空 |
| int size() | 回傳集合的尺寸 |
例: 創建集合
//創建Collection集合物件
Collection<String> col=new ArrayList<String>();
//Boolean add(E e) 添加元素
col.add("java265.com-1");
col.add("java265.com-2");
//boolean remove(E e) 從集合中移除指定的元素
col.remove("java265.com-2");
//void clear() 清空集合中的元素
col.clear();
//bool contains(E e) 判斷集合中是否存在指定的元素
col.contains("java265.com-3");
//boolean isEmpty() 判斷集合是否為空
col.isEmpty();
//int size() 集合的長度
col.size();
//遍歷集合方式一
Iterator<String> it=col.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//遍歷集合方式二
Object[] objects=col.toArray();
for (int i=0;i<objects.length;i++){
System.out.println(objects[i]);
}
//遍歷集合方式三
for (String str :
col) {
System.out.println(str);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/500066.html
標籤:其他
上一篇:Java內部類簡介說明
