[java]代码库
import java.util.ArrayList;
import java.util.List;
//主函数
public class ArrayListDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
printString();
printUserName();
}
public static void printUserName() {
List<User> userList = new ArrayList<User>();
User user = new User();
user.setName("教师");
userList.add(user);
user.setName("学生");
userList.add(user);
System.out.println(userList.get(0).getName());//结果:学生
System.out.println(userList.get(1).getName());//结果:学生
}
public static void printString() {
List<String> userList = new ArrayList<String>();
String str = new String();
str="教师";
userList.add(str);
str="学生";
userList.add(str);
System.out.println(userList.get(0).toString());//结果:教师
System.out.println(userList.get(1).toString());//结果:学生
}
}
//类User
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}