当前位置: 首页 > news >正文

[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

代码如下:

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;}
}

http://www.dtcms.com/a/430809.html

相关文章:

  • 网站建设背景及目的搜索优化的培训免费咨询
  • 2012 年真题配套词汇单词笔记(考研真相)
  • Ubuntu20.04 按照飞浆paddle 3.2遇到的问题
  • 网站建设推荐郑国华做网站ps图片都是多大
  • 探索 Docker/K8s 部署 MySQL 的创新实践与优化技巧
  • 线程属性的相关设置详解
  • 深圳公明网站建设桂林北站到阳朔
  • maven的概述以及在mac安装配置
  • 【复习】计网强化第一章
  • 【微信公众平台】小程序如何查找菜单?如何通过自定义的菜单路径生成小程序二维码?小程序二维码指定生成
  • 瑞萨M85内核芯片再出1GHz旗舰双核新品RA8T2,两个千兆以太网MAC,集成EtherCAT从机接口,面向高端电机控制
  • 海洋公园网站建设方案网站开发加设计要多少钱
  • KingbaseES 的 SQL Server 兼容性测试
  • 基于ps2021实现1寸相纸的打印
  • [论文阅读] AI + 软件工程 | 从“事后补救”到“实时防控”,SemGuard重塑LLM代码生成质量
  • 购物网站修改文案常见的网络推广方法有几种
  • 手腕鼓包?可能是腱鞘囊肿
  • 网站推广方法有网站制作哪家做的好
  • Servlet 国际化
  • 安卓基础组件016--Toas组件
  • InfiniBand技术解析(2):为什么它是高性能计算的 “超级血管”?
  • 微商本地化发展模式的借鉴与探讨——以开源AI智能名片链动2+1模式S2B2C商城小程序为例
  • 基于遗传优化的LSTM-Attention一维时间序列预测算法matlab仿真
  • 深圳公司 网站建设广州做网站公司哪家好
  • 多个wordpress网站合并成品源码1688网站免费
  • macOS/Linux ClaudeCode 安装指南及 Claude Sonnet 4.5 介绍
  • [创业之路-640]:通信行业供应链 - 通信网的发展趋势:IP化统一 、云网融合 、算网协同 、FMC(固定移动融合)、空天地一体化
  • IP 地址管理:IPv4 和 IPv6 地址规划、子网划分与 CIDR
  • router-id <ip-address> 概念及题目
  • Linux应用(6)——网络通信/TCP/IP