基本方法
public class ArrayListTest1 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add("hello");
arrayList.add("world");
arrayList.add("world");
arrayList.add("welcome");
String s1 = (String) arrayList.get(0);
String s2 = (String) arrayList.get(1);
String s3 = (String) arrayList.get(2);
String s4 = (String) arrayList.get(3);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("-------------");
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
// arrayList.clear();
// System.out.println(arrayList.size());
// System.out.println(arrayList.isEmpty());
arrayList.remove(0);
System.out.println("-------------");
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
arrayList.remove("welcome");
System.out.println("-------------");
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
System.out.println("-----------------");
arrayList.add("aaa");
arrayList.add("bbb");
System.out.println(arrayList.indexOf("world"));
System.out.println(arrayList.lastIndexOf("world"));
System.out.println(arrayList.indexOf("aaa"));
}
}
结果
hello
world
world
welcome
-------------
hello
world
world
welcome
-------------
world
world
welcome
-------------
world
world
-----------------
0
1
2
包装类与原生数据类型测试
public class ArrayListTest2 {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("hello");
list.add(new Integer(2));
String str = (String)list.get(0);
Integer in = (Integer)list.get(1);
// String str2 = (String)list.get(1);
System.out.println(str);
System.out.println(in.intValue());
// System.err.println(str2);
}
}
结果
hello
2
包装类型的遍历与操作
public class ArrayListTest3 {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(new Integer(3));
list.add(new Integer(4));
list.add(new Integer(5));
list.add(new Integer(6));
int sum = 0;
for (int i = 0; i < list.size(); i++) {
Integer item = (Integer) list.get(i);
sum += item.intValue();
}
System.out.println(sum);
}
}
结果
18
ArrayList与数组之间的转换
public class ArrayListTest4 {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
list.add(new Integer(4));
list.add(new Integer(5));
list.add(new Integer(6));
/**
* 不能将Object[]转换为Integer[]
*/
// 这个地方会报错,类型转换异常
// Integer[] in = (Integer[]) list.toArray();
// for (int i = 0; i < in.length; i++) {
// Integer integer = in[i];
// System.out.println(integer.intValue());
// }
Object[] in = list.toArray();
for (int i = 0; i < in.length; i++) {
Integer integer = (Integer) in[i];
System.out.println(integer.intValue());
}
}
}
结果
1
2
3
4
5
6
ArrayList操作自定义对象
public class ArrayListTest5 {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(new Point(2, 3));
list.add(new Point(2, 2));
list.add(list);
list.add(new Point(4, 4));
// for (int i = 0; i < list.size(); i++) {
// System.out.println((Point) list.get(i));
// }
System.out.println(list.toString());
}
}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "x=" + this.x + ",y=" + this.y;
}
}
结果
[x=2,y=3, x=2,y=2, (this Collection), x=4,y=4]
标签:ArrayList,list,System,println,add,深入分析,arrayList,out
From: https://www.cnblogs.com/tangyouwei/p/arraylist-in-depth-analysis-z18oerm.html