ArrayList

1.ArrayList:长度可变的数组,集合对象。

(1)实例化方法:

ArrayList stringList = new ArrayList ();

2.全局变量:再类范围内,所有方法都可见

  局部变量:在方法内部定义

3.

创建学生类(代码):

public class Student {
	private String name;
	private String className;
	private int age;
	
	public Student(String name, String className, int age) {
		super();
		this.name = name;
		this.className = className;
		this.age = age;	
		
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}


}

学生测试类(代码):

public class Test {
public static void main(String[] args) {
	ArrayList studentList= new ArrayList<>();
	Student s1 = new Student();
	s1.setName("小明");
	s1.setClassName("一年级一班");
	s1.setAge(8);
	
	Student s2 = new Student("小红","二年级二班",9);
	Student s3 = new Student("小花","二年级二班",9);
	Student s4 = new Student("小白","二年级二班",9);
	Student s5 = new Student("小华","二年级二班",9);
	
	studentList.add(s1);
	studentList.add(s2);
	studentList.add(s3);
	studentList.add(s4);
	studentList.add(s5);
	
	for (Student stu : studentList) {
		System.out.println("姓名:"+stu.getName()+",年级:"+stu.getClassName()+"年龄:"+stu.getAge()+"岁");
	}
	studentList.remove(studentList.size()-2);
	System.out.println();
	for(int i=0;i


 
  

你可能感兴趣的:(ArrayList)