标签:
//创建ArrayList
ArrayList arr = new ArrayList();
//ArrayList添加数据
arr.add("123");
arr.add("234");
arr.add("345");
arr.add("456");
arr.add("567");
arr.add("678");
//ArrayList转化为数组
Object[] strArr = arr.toArray();
System.out.println(strArr.length);
System.out.println("-----------------------");
for (int i=0;i<strArr.length;i++){
System.out.println(strArr[i]);
}
//获取ArrayList的迭代器iterator
Iterator iterator = arr.iterator();
System.out.println("-----------------------");
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("-----------------------");
//ArrayList中的移除数据
boolean result = arr.remove("123");
System.out.println(result);
for (int i=0;i<arr.size();i++){
System.out.println(arr.get(i));
}
System.out.println("-----------------------");
//ArrayList插入数据
arr.set(0,"123");
for (int i=0;i<arr.size();i++){
System.out.println(arr.get(i));
}
System.out.println("-----------------------");
ArrayList arr2 = new ArrayList(3);
arr2.add("abc");
arr2.add("bcd");
arr2.add("cde");
//ArrayList添加一个集合
arr.addAll(arr2);
for (int i=0;i<arr.size();i++){
System.out.println(arr.get(i));
}
System.out.println("-----------------------");
//获取ArrayList的元素个数
int size = arr.size();
//判断ArrayList是否为空
boolean result1 = arr.isEmpty();
System.out.println(size);
System.out.println(result1);
System.out.println("-----------------------");
//Arr判断ArrayList是否包含abc
boolean result3 = arr.contains("abc");
System.out.println(result3);
System.out.println(arr.containsAll(arr2));
//获取ArrayList中的对象的索引
int index = arr.indexOf("abc");
System.out.println(index);
//克隆ArrayList
Object ob = arr.clone();
System.out.println(ob);
System.out.println("-----------------------");
//获取ArrayList中对应索引的对象
Object obj = arr.get(3);
System.out.println(obj);
System.out.println("-----------------------");
//清除ArrayList中的所有元素
arr2.clear();
System.out.println(arr2);
System.out.println("-----------------------");
标签:
原文地址:http://www.cnblogs.com/jianbo-su/p/5843139.html