[Java]PTA: jmu-Java-02基本语法-08-ArrayList入门

本习题主要用于练习如何使用ArrayList来替换数组。

新建1个ArrayList<String> strList用来存放字符串,然后进行如下操作。

提示: 查询Jdk文档中的ArrayList。
注意: 请使用System.out.println(strList)输出列表元素。

输入格式

  1. 输入: 若干字符串,放入strList。直到输入为!!end!!时,结束输入。

  2. strList头部新增一个begin,尾部新增一个end

  3. 输出列表元素

  4. 输入: 字符串str

  5. 判断strList中有无包含字符串str,如包含输出true,否则输出false。并且输出下标,没包含返回-1。

  6. 在strList中从后往前找。返回其下标,找不到返回-1。

  7. 移除掉第1个(下标为0)元素,并输出。然后输出列表元素。

  8. 输入: 字符串str

  9. 将第2个(下标为1)元素设置为字符串str.

  10. 输出列表元素

  11. 输入: 字符串str

  12. 遍历strList,将字符串中包含str的元素放入另外一个ArrayList strList1,然后输出strList1。

  13. 在strList中使用remove方法,移除第一个和str相等的元素。

  14. 输出strList列表元素。

  15. 使用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

代码如下:

java 复制代码
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) 方法的作用是:判断当前字符串中是否包含指定的子字符串,而不是判断字符串是否完全等于指定内容。

java 复制代码
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。
java 复制代码
// 示例:查找元素索引
ArrayList<String> fruits = new ArrayList<>();
fruits.add("苹果");
fruits.add("香蕉");
fruits.add("苹果"); // 重复元素

int firstApple = fruits.indexOf("苹果");
System.out.println("苹果第一次出现的索引:" + firstApple); // 输出:0

int lastApple = fruits.lastIndexOf("苹果");
System.out.println("苹果最后一次出现的索引:" + lastApple); // 输出:2

int orangeIndex = fruits.indexOf("橙子");
System.out.println("橙子的索引:" + orangeIndex); // 输出:-1(不存在)

4、增强 for 循环中不能直接删除元素,应该用迭代器或普通 for 循环。

这是因为:集合的迭代器有一个快速失败(fail-fast)机制:当迭代器正在遍历集合时,如果通过集合自身的方法(如 remove())修改了集合的结构(添加 / 删除元素),迭代器会检测到这种 "并发修改",并立即抛出 ConcurrentModificationException。那为什么普通迭代器可以呢,因为普通迭代器自身提供了 remove() 方法,它在删除元素的同时,会同步更新迭代器的内部状态。

1)普通迭代器

java 复制代码
Iterator<String> it = strList.iterator();
while (it.hasNext()) {
    String e = it.next();
    if (e.equals(str3)) {
        it.remove(); // 迭代器自己的删除方法,不会报错
        break;
    }
}

2)普通 for 循环(唯一需要注意的就是删除元素后手动调整索引)

java 复制代码
for (int i = 0; i < strList.size(); i++) {
    if (strList.get(i).equals(str3)) {
        strList.remove(i); // 通过索引删除
        i--; // 关键:删除后索引减1,避免跳过下一个元素
        break;
    }
}
相关推荐
235162 小时前
【MySQL】数据库事务深度解析:从四大特性到隔离级别的实现逻辑
java·数据库·后端·mysql·java-ee
ClassOps3 小时前
腾讯CODING Maven的aar制品添加上传流程
android·java·maven
zzxxlty3 小时前
maven install依赖后 另一个项目 maven reload找不到包
java·maven·intellij-idea
ClassOps3 小时前
基于腾讯CODING Maven的Android库发布
android·java·maven
xlq223223 小时前
12.排序(上)
数据结构·算法·排序算法
何中应3 小时前
MyBatis-Plus字段类型处理器使用
java·数据库·后端·mybatis
聪明的笨猪猪4 小时前
Java SE “面向对象”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
努力学习的小廉4 小时前
我爱学算法之—— 分治-快排
c++·算法
未知陨落4 小时前
LeetCode:77.买卖股票的最佳时机
算法·leetcode