ArrayList
一、ArrayList创建变量的步骤
- 1.导入包
import java.util.*;
- 2.创建引用类型的变量
数据类型<集合存储的数据类型> 变量名 = new 数据类型<集合存储的数据类型>();
集合存储的数据类型:要将数据存储到集合的容器中。创建集合引用变量的时候,必须要指定好存储的类型是什么。
- 3.变量名.方法即可调用
注意尖括号中的类型必须要写。
注意:ArrayList存储的是引用类型,那么8个基本的类型对应8个引用类型。
如下表所示:
二、ArrayList常见的方法
- array.add(object);//添加一个元素
- array.get(index);//取出集合中的元素,在get方法的参数中,写入索引。
- array.size();//返回集合的长度,也就是存储元素的个数。
- array.set(object);//设置一个元素
- array.remove();//移除一个元素
三、举例
使用学生数组,存储三个学生对象,代码如下:
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name,int age){
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;
}
}
public class Test01ArrayList {
public static void main(String[] args) {
//创建学生数组
Student[] students = new Student[3];
//创建学生对象
Student s1 = new Student("曹操", 40);
Student s2 = new Student("刘备", 35);
Student s3 = new Student("孙权", 30);
//把学生对象作为元素赋值给学生数组
students[0] = s1;
students[1] = s2;
students[2] = s3;
//遍历学生数组
for (int x = 0; x < students.length; x++) {
Student s = students[x];
System.out.println(s.getName() + "‐‐‐" + s.getAge());
}
}
}
使用ArrayList类,存储三个字符串对象,代码如下:
public static void main(String[] args) {
//创建学生数组
ArrayList<String> list = new ArrayList<>();
//创建学生对象
String s1 = "曹操";
String s2 = "刘备";
String s3 = "孙权";
//打印学生ArrayList集合
System.out.println(list);
// 把学生对象作为元素添加到集合
list.add(s1);
list.add(s2);
list.add(s3);
// 打印学生ArrayList集合
System.out.println(list);
}
数值添加到集合
生成6个1~33之间的随机整数,添加到集合,并遍历
public class Test01ArrayList {
public static void main(String[] args) {
// 创建Random 对象
Random random = new Random();
// 创建ArrayList 对象
ArrayList<Integer> list = new ArrayList<>();
// 添加随机数到集合
for (int i = 0; i < 6; i++) {
int r = random.nextInt(33) + 1;
list.add(r);
}
// 遍历集合输出
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
对象添加到集合
自定义4个学生对象,添加到集合,并遍历
public static void main(String[] args) {
//创建学生对象放入ArrayList集合
ArrayList<Student> list = new ArrayList<>();
//创建学生对象
Student s1 = new Student("赵丽颖", 18);
Student s2 = new Student("唐嫣", 20);
Student s3 = new Student("景甜", 25);
Student s4 = new Student("柳岩", 19);
//把学生对象作为元素添加到集合中
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
//遍历集合
for (int x = 0; x < list.size(); x++) {
Student s = list.get(x);
System.out.println(s.getName() + "‐‐‐" + s.getAge());
}
}
打印集合方法
定义以指定格式打印集合的方法
(ArrayList
类型作为参数
)
,使用
{}
扩起集合,使用
@
分隔每个元素。格式参照
{
元素
@
元素
@
元素
}
。
public static void main(String[] args) {
//创建学生数组
ArrayList<String> list = new ArrayList<>();
// 添加字符串到集合中
list.add("张三丰");
list.add("宋远桥");
list.add("张无忌");
list.add("殷梨亭");
//调用方法
printArrayList(list);
}
public static void printArrayList(ArrayList<String> list) {
// 拼接左括号
System.out.print("{");
// 遍历集合
for (int i = 0; i < list.size(); i++) {
// 获取元素
String s = list.get(i);
// 拼接@符号
if (i != list.size() - 1) {
System.out.print(s + "@");
} else {
// 拼接右括号
System.out.print(s + "}");
}
}
}
获取集合方法
定义获取所有偶数元素集合的方法
(ArrayList
类型作为返回值
)
public static void main(String[] args) {
// 创建Random 对象
Random random = new Random();
// 创建ArrayList 对象
ArrayList<Integer> list = new ArrayList<>();
// 添加随机数到集合
for (int i = 0; i < 20; i++) {
int r = random.nextInt(100) + 1;
list.add(r);
}
// 调用偶数集合的方法
ArrayList<Integer> arrayList = getArrayList(list);
System.out.println(arrayList);
}
public static ArrayList<Integer> getArrayList(ArrayList<Integer> list) {
// 创建小集合,来保存偶数
ArrayList<Integer> smallList = new ArrayList<>();
// 遍历list
for (int i = 0; i < list.size(); i++) {
// 获取元素
Integer num = list.get(i);
// 判断为偶数,添加到小集合中
if (num % 2 == 0) {
smallList.add(num);
}
} // 返回小集合
return smallList;
}