package com.lee;
import java.util.*;
public class ArrayListTest1
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("hello");
arrayList.add("world");
arrayList.add("welcome");
String s1 = (String)arrayList.get(0);
String s2 = (String)arrayList.get(1);
String s3 = (String)arrayList.get(2);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println("-----------------------");
for (int i = 0; i < arrayList.size(); i++) //用于获取数组的个数
{
System.out.println(arrayList.get(i));
}
}
}
toarry的方法应用
package com.lee;
import java.util.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));
Object[] in = list.toArray();
for(int i = 0; i < in.length; i++)
{
System.out.println(((Integer)in[i]).intValue());
}
}
}
package com.lee; import java.util.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(new Point(5, 5)); for(int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println(list); } } class Point { int x; int y; Point (int x, int y) { this.x = x; this.y = y; } public String toString() { return "x=" + this.x + "," +"y="+ this.y ; } }