栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

3-2 JAVA [集合-List中指定元素的删除]

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

3-2 JAVA [集合-List中指定元素的删除]

编写以下两个函数

//以空格(单个或多个)为分隔符,将line中的元素抽取出来,放入一个List
public static List convertStringToList(String line) 
//在list中移除掉与str内容相同的元素
public static void remove(List list, String str)

输入样例
1 2 1 2 1 1 1 2
1
11 1 11 1 11
11
2 2 2 
1
1   2 3 4 1 3 1
1

输出样例
[1, 2, 1, 2, 1, 1, 1, 2]
[2, 2, 2]
[11, 1, 11, 1, 11]
[1, 1]
[2, 2, 2]
[2, 2, 2]
[1, 2, 3, 4, 1, 3, 1]
[2, 3, 4, 3]

参考代码:

import java.util.*;
public class Main {
	public static List convertStringToList(String line){
		Listlist=new ArrayList();
		String str[]=line.split("\s+");
		
		for(int i=0;i list, String str){
		for(int i=0;i list = convertStringToList(sc.nextLine());
            System.out.println(list);
            String word = sc.nextLine();
            remove(list,word);
            System.out.println(list);
        }
        sc.close();
    }

}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/868254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号