有梦青年初学JAVA(七)---常用API之ArrayList类

ArrayList类

1.初步引入 – 对象数组

使用学生数组,存储三个学生对象,代码如下:

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;
}
publicint getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Test01StudentArray {
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());
}
}
}

到⽬前为⽌,我们想存储对象数据,选择的容器,只有对象数组。⽽数组的⻓度是固定的,⽆法适应数据变化的需求。为了解决这个问题,Java提供了另⼀个容器 java.util.ArrayList集合类,让我们可以更便捷的存储和操作对象数据。

2. 什么是ArrayList类

java.util.ArrayList 是⼤⼩可变的数组的实现,存储在内的数据称为元素。此类提供⼀些⽅法来操作内部存储的元素。 ArrayList 中可不断添加元素,其⼤⼩也⾃动增⻓。

3.Arraylist使用步骤看类

查看类
java.util.ArrayList < E> :该类需要import导⼊使后使⽤。
< E>表示⼀种指定的数据类型,叫做泛型。 E ,取自Element(元素)的⾸字⺟。在出现 E 的地⽅,我们是用一种引用数据类型将其代替即可,表示我们将存储哪种引用类型的元素。代码如下:

ArrayList<String>,ArrayList<Student> 

在JDK 7后,右侧泛型的尖括号之内可以留空,但是<>仍然要写。简化格式:

ArrayList<String> list = new ArrayList<>();

查看成员⽅法
public boolean add(E e) :将指定的元素添加到此集合的尾部。
参数 E e,在构造ArrayList对象时, < E> 指定了什么数据类型,那么 add(E e) ⽅法中,只能添加什么数据类型的对象。
使⽤ArrayList类,存储三个字符串元素,代码如下:

public class Test02StudentArrayList {
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);
}
}

4.常用方法和遍历

对于元素的操作,基本体现在 – 增、删、查。
常⽤的⽅法有:
public boolean add(E e) :将指定的元素添加到此集合的尾部。
public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。
public E get(int index) :返回此集合中指定位置上的元素。返回获取的元素。
public int size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防⽌越
界。
这些都是最基本的⽅法,操作⾮常简单,代码如下:

public class Demo01ArrayListMethod {
public static void main(String[] args) {
// 创建集合对象
ArrayList<String> list = new ArrayList<String>();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// public E get(int index):返回指定索引处的元素
System.out.println("get:" + list.get(0));
System.out.println("get:" + list.get(1));
System.out.println("get:" + list.get(2));
// public int size():返回集合中的元素的个数
System.out.println("size:" + list.size());
// public E remove(int index):删除指定索引处的元素,返回被删除的元素
System.out.println("remove:" + list.remove(0));
// 遍历输出
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}

5.如何存储基本数据类型

ArrayList对象不能存储基本类型,只能存储引⽤类型的数据。类似 不能写,但是存储基
本数据类型对应的包装类型是可以的。所以,想要存储基本类型数据, <> 中的数据类型,必须
转换后才能编写,转换写法如下:

基本类型基本类型
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

我们发现,只有 Integer 和 Character 需要特殊记忆,其他基本类型只是⾸字⺟⼤写即可。那么
存储基本类型数据,代码如下:

public class Demo02ArrayListMethod {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println(list);
}
}

6.ArrayList练习

数值添加到集合

⽣成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 class Test02ArrayList {
public static void main(String[] args) {
// 创建集合对象
ArrayList<Student> list = new ArrayList<Student>();
// 创建学⽣对象
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 class Test03ArrayList {
public static void main(String[] args) {
// 创建集合对象
ArrayList<String> list = new ArrayList<String>();
// 添加字符串到集合中
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 class Test04ArrayList {
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(1000) + 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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值