1.定义:ArrayList是一个动态数组,该数组的容量能任意变化(增大或减少)。
2.使用方法:ArrayList <类名1> 数组名=new ArrayList<类名2>();
(注意:类名1和类名2是相同的,类名2可以省略)
3.例子:
```
例子1
package T1;
import java.util.ArrayList;
public class demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> str = new ArrayList<String>();
str.add("hey");
str.add("hello");
str.add("hi");
System.out.println(str);
for (int i = 0; i < str.size(); i++) {
// String s = str.get(i);
System.out.print(str.get(i) + " ");
}
}
}
```
```
例子2
//创建一个类
package P02;
public class Student {
String name;
String gender;
int age;
String classname;
Student() {
// TODO Auto-generated constructor stub
}
/**
*
* @param name
* @param gender
* @param age
* @param classname
*/
public Student(String name, String gender, int age, String classname) {
this.name = name;
this.gender = gender;
this.age = age;
this.classname = classname;
}
}
//利用创建好的类创建数组
package P02;
import java.util.ArrayList;
public class Text {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> students = new ArrayList<Student>();
Student t1 = new Student("jack", "男", 22, "2班");
Student t2 = new Student("frank", "男", 23, "3班");
Student t3 = new Student("marry", "女", 24, "4班");
students.add(t1);
students.add(t2);
students.add(t3);
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println("学生姓名:"+ s.name+" 学生性别:"+s.gender+" 学生年龄:"+s.age+" 学生班级:"+s.classname);
}
/*for (Student stu : students) {
System.out.println(stu.classname+"--"+stu.gender);
}
*/
}
}
```
2.使用方法:ArrayList <类名1> 数组名=new ArrayList<类名2>();
(注意:类名1和类名2是相同的,类名2可以省略)
3.例子:
```
例子1
package T1;
import java.util.ArrayList;
public class demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> str = new ArrayList<String>();
str.add("hey");
str.add("hello");
str.add("hi");
System.out.println(str);
for (int i = 0; i < str.size(); i++) {
// String s = str.get(i);
System.out.print(str.get(i) + " ");
}
}
}
```
```
例子2
//创建一个类
package P02;
public class Student {
String name;
String gender;
int age;
String classname;
Student() {
// TODO Auto-generated constructor stub
}
/**
*
* @param name
* @param gender
* @param age
* @param classname
*/
public Student(String name, String gender, int age, String classname) {
this.name = name;
this.gender = gender;
this.age = age;
this.classname = classname;
}
}
//利用创建好的类创建数组
package P02;
import java.util.ArrayList;
public class Text {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> students = new ArrayList<Student>();
Student t1 = new Student("jack", "男", 22, "2班");
Student t2 = new Student("frank", "男", 23, "3班");
Student t3 = new Student("marry", "女", 24, "4班");
students.add(t1);
students.add(t2);
students.add(t3);
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println("学生姓名:"+ s.name+" 学生性别:"+s.gender+" 学生年龄:"+s.age+" 学生班级:"+s.classname);
}
/*for (Student stu : students) {
System.out.println(stu.classname+"--"+stu.gender);
}
*/
}
}
```