7-1 jmu-Java-02基本语法-08-ArrayList入门 (20分)

这篇博客介绍了如何在Java中使用ArrayList作为数据结构替代数组。通过一个实例展示了ArrayList的创建、添加元素、输出列表以及查找、删除操作。内容包括对ArrayList的基本操作和样例代码的解释。
摘要由CSDN通过智能技术生成

本习题主要用于练习如何使用ArrayList来替换数组。
新建1个ArrayList strList用来存放字符串,然后进行如下操作。
提示:查询Jdk文档中的ArrayList。
注意:请使用System.out.println(strList)输出列表元素。

输入格式

输入n个字符串,放入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 12b c d !!end!!
b1
second
b

输出样例:

[begin, a1, b1, 3b, a2, b2, 12b, c, d, end]
true
2
2
begin
[a1, b1, 3b, a2, b2, 12b, c, d, end]
[a1, second, 3b, a2, b2, 12b, c, d, end]
[3b, b2, 12b]
[a1, second, 3b, a2, b2, 12b, c, d, end]
[],0,true

import java.util.Scanner;
import java.util.ArrayList;

public class Main {
   

     public static void main(String[] args) {
   
        Scanner se = new Scanner(System.in);
        ArrayList<String> strList =new ArrayList<String>();
        while (true) {
   
        	String s=se.next();
        	if(s.equals("!!end!!")) {
   
        		break;
        	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值