博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7-1 jmu-Java-02基本语法-08-ArrayList入门 (20分)
阅读量:4036 次
发布时间:2019-05-24

本文共 2026 字,大约阅读时间需要 6 分钟。

本习题主要用于练习如何使用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
strList =new ArrayList
(); while (true) {
String s=se.next(); if(s.equals("!!end!!")) {
break; }else {
strList.add(s); } } strList.add(0, "begin"); strList.add("end"); System.out.println(strList); String str=se.next(); stepFive(strList,str); boolean b=true; for(int i=strList.size()-1;i>=0;i--) {
if(strList.get(i).equals(str)) {
System.out.println(i); b=false; break; } } if(b==true) {
System.out.println(-1); } System.out.println(strList.get(0)); strList.remove(0); System.out.println(strList); String e=se.next(); strList.set(1, e); System.out.println(strList); String w=se.next(); ArrayList
a =new ArrayList
(); for(int i=0;i
strList,String str) {
boolean w=true; int u=0; for(int i=0;i

转载地址:http://kqbdi.baihongyu.com/

你可能感兴趣的文章
YUV420只绘制Y通道
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt5 everywhere编译完成后,找不到qmake
查看>>
arm-linux开机读取硬件时钟,设置系统时钟。
查看>>
交叉编译在x86上调试好的qt程序
查看>>
/dev/input/event0 键盘输入
查看>>
qt 创建异形窗体
查看>>
可重入函数与不可重入函数
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
输入设备节点自动生成
查看>>
opencv test code-1
查看>>
eclipse 导入先前存在的项目
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
busybox passwd修改密码
查看>>