package com.hanqi;
import java.util.ArrayList;
public class TestList {
public static void main(String[] args) {
int[] a;
//ArrayList 可变数组
ArrayList<String> array = new ArrayList<String>();
array.add("abc");//放数据
array.add("def");
for(int i=0;i<10;i++){
//array.add(i+"");
array.add(new Integer(i).toString());
}
System.out.println("集合的长度 = "+array.size());
System.out.println(array.get(1));
System.out.println(" ");
for(String t:array){
System.out.println("t = "+t);
}
ArrayList<User> a1User = new ArrayList<User>();
a1User.add(new User("小明",10));//匿名对象
User aUser = new User("小李",20);
a1User.add(aUser);
for(int i=0;i<a1User.size();i++){
User u = a1User.get(i);
System.out.println(""+u);
}
}
}
package com.hanqi;
public class User {
private String name;
private int age;
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}