[Java]PTA: jmu-Java-02基本语法-08-ArrayList入门
本习题主要用于练习如何使用ArrayList来替换数组。
新建1个ArrayList<String> strList
用来存放字符串,然后进行如下操作。
提示: 查询Jdk文档中的ArrayList。
注意: 请使用System.out.println(strList)
输出列表元素。
输入格式
输入: 若干字符串,放入
strList
。直到输入为!!end!!
时,结束输入。在
strList
头部新增一个begin
,尾部新增一个end
。输出列表元素
输入: 字符串
str
判断
strList
中有无包含字符串str
,如包含输出true
,否则输出false
。并且输出下标,没包含返回-1。在strList中从后往前找。返回其下标,找不到返回-1。
移除掉第1个(下标为0)元素,并输出。然后输出列表元素。
输入: 字符串str
将第2个(下标为1)元素设置为字符串str.
输出列表元素
输入: 字符串str
遍历strList,将字符串中包含str的元素放入另外一个
ArrayList strList1
,然后输出strList1。在strList中使用
remove
方法,移除第一个和str相等的元素。输出strList列表元素。
使用
clear
方法,清空strList。然后输出strList的内容,size()
与isEmpty()
,3者之间用,
连接。
输入样例:
a1 b1 3b a2 b2 b1 12b c d !!end!!
b1
second
b
输出样例:
[begin, a1, b1, 3b, a2, b2, b1, 12b, c, d, end]
true
2
6
begin
[a1, b1, 3b, a2, b2, b1, 12b, c, d, end]
[a1, second, 3b, a2, b2, b1, 12b, c, d, end]
[3b, b2, b1, 12b]
[a1, second, 3b, a2, b2, b1, 12b, c, d, end]
[],0,true
代码如下:
import java.util.ArrayList;
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner sc=new Scanner(System.in);ArrayList<String> strList=new ArrayList<>();strList.add("begin");while(true){String temp=sc.next();if(temp.equals("!!end!!"))break;strList.add(temp);}strList.add("end");System.out.println(strList);String str1=sc.next();System.out.println(strList.contains(str1));System.out.println(strList.indexOf(str1));System.out.println(strList.lastIndexOf(str1));System.out.println(strList.get(0));strList.remove(0);System.out.println(strList);String str2=sc.next();strList.set(1,str2);System.out.println(strList);String str3=sc.next();ArrayList<String> strList1=new ArrayList<>();for(String e:strList){if(e.contains(str3)){strList1.add(e);}}System.out.println(strList1);/*增强for循环中不能直接删除元素,应该用迭代器或普通for循环for(String e:strList){if(e.equals(str3)){strList.remove(e);break;}}*/for(int i=0;i<strList.size();i++){if(strList.get(i).equals(str3)){strList.remove(i);break;}}System.out.println(strList);strList.clear();System.out.println(strList+","+strList.size()+","+strList.isEmpty());sc.close();}
}
做题反思:
1、ArrayList 只能存储引用数据类型(如 Integer、String、Person 等),不能直接存储基本数据类型(如 int、double、boolean 等)。因为 Java 集合框架(包括 ArrayList)的设计初衷是处理对象,其内部存储的是对象的引用,而基本数据类型(如 int)不是对象,因此无法直接放入 ArrayList 中。
2、String 类的 contains(CharSequence s) 方法的作用是:判断当前字符串中是否包含指定的子字符串,而不是判断字符串是否完全等于指定内容。
String str1 = "bad";
String str2 = "b";// 判断"bad"中是否包含"b"
System.out.println(str1.contains("b")); // 输出 true(因为"bad"包含子串"b")// 判断"b"中是否包含"b"
System.out.println(str2.contains("b")); // 输出 true(完全匹配也属于包含)// 判断"bad"中是否包含"x"
System.out.println(str1.contains("x")); // 输出 false(不包含)
3、使用 indexOf() 或 lastIndexOf():查找元素位置。
- indexOf(Object o):返回元素在集合中第一次出现的索引,若不存在则返回 -1。
- lastIndexOf(Object o):返回元素在集合中最后一次出现的索引,若不存在则返回 -1。
// 示例:查找元素索引
ArrayList<String> fruits = new ArrayList<>();
fruits.add("苹果");
fruits.add("香蕉");
fruits.add("苹果"); // 重复元素int firstApple = fruits.indexOf("苹果");
System.out.println("苹果第一次出现的索引:" + firstApple); // 输出:0int lastApple = fruits.lastIndexOf("苹果");
System.out.println("苹果最后一次出现的索引:" + lastApple); // 输出:2int orangeIndex = fruits.indexOf("橙子");
System.out.println("橙子的索引:" + orangeIndex); // 输出:-1(不存在)
4、增强 for 循环中不能直接删除元素,应该用迭代器或普通 for 循环。
这是因为:集合的迭代器有一个快速失败(fail-fast)机制:当迭代器正在遍历集合时,如果通过集合自身的方法(如 remove())修改了集合的结构(添加 / 删除元素),迭代器会检测到这种 “并发修改”,并立即抛出 ConcurrentModificationException。那为什么普通迭代器可以呢,因为普通迭代器自身提供了 remove() 方法,它在删除元素的同时,会同步更新迭代器的内部状态。
1)普通迭代器
Iterator<String> it = strList.iterator();
while (it.hasNext()) {String e = it.next();if (e.equals(str3)) {it.remove(); // 迭代器自己的删除方法,不会报错break;}
}
2)普通 for 循环(唯一需要注意的就是删除元素后手动调整索引)
for (int i = 0; i < strList.size(); i++) {if (strList.get(i).equals(str3)) {strList.remove(i); // 通过索引删除i--; // 关键:删除后索引减1,避免跳过下一个元素break;}
}