Java中对List集合的常用操作(转载)
目录:
- list中添加,获取,删除元素;
- list中是否包含某个元素;
- list中根据索引将元素数值改变(替换);
- list中查看(判断)元素的索引;
- 根据元素索引位置进行的判断;
- 利用list中索引位置重新生成一个新的list(截取集合);
- 对比两个list中的所有元素;
- 判断list是否为空;
- 返回Iterator集合对象;
- 将集合转换为字符串;
- 将集合转换为数组;
- 集合类型转换;
- 去重复;
备注:内容中代码具有关联性。
1.list中添加,获取,删除元素;
添加方法是:.add(e); 获取方法是:.get(index); 删除方法是:.remove(index); 按照索引删除; .remove(Object o); 按照元素内容删除;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
List<String> person= new ArrayList<>(); person.add( "jackie" ); //索引为0 //.add(e) person.add( "peter" ); //索引为1 person.add( "annie" ); //索引为2 person.add( "martin" ); //索引为3 person.add( "marry" ); //索引为4 person.remove( 3 ); //.remove(index) person.remove( "marry" ); //.remove(Object o) String per= "" ; per=person.get( 1 ); System.out.println(per); ////.get(index) for ( int i = 0 ; i < person.size(); i++) { System.out.println(person.get(i)); //.get(index) } |
2.list中是否包含某个元素;
方法:.contains(Object o); 返回true或者false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
List<String> fruits= new ArrayList<>(); fruits.add( "苹果" ); fruits.add( "香蕉" ); fruits.add( "桃子" ); //for循环遍历list for ( int i = 0 ; i < fruits.size(); i++) { System.out.println(fruits.get(i)); } String appleString= "苹果" ; //true or false System.out.println( "fruits中是否包含苹果:" +fruits.contains(appleString)); if (fruits.contains(appleString)) { System.out.println( "我喜欢吃苹果" ); } else { System.out.println( "我不开心" ); } |
3.list中根据索引将元素数值改变(替换);
注意 .set(index, element); 和 .add(index, element); 的不同;
1
2
3
4
5
6
7
8
9
10
11
12
|
String a= "白龙马" , b= "沙和尚" , c= "八戒" , d= "唐僧" , e= "悟空" ; List<String> people= new ArrayList<>(); people.add(a); people.add(b); people.add(c); people.set( 0 , d); //.set(index, element); //将d唐僧放到list中索引为0的位置,替换a白龙马 people.add( 1 , e); //.add(index, element); //将e悟空放到list中索引为1的位置,原来位置的b沙和尚后移一位 //增强for循环遍历list for (String str:people){ System.out.println(str); } |
4.list中查看(判断)元素的索引;
注意:.indexOf(); 和 lastIndexOf()的不同;
1
2
3
4
5
6
7
8
9
10
|
List<String> names= new ArrayList<>(); names.add( "刘备" ); //索引为0 names.add( "关羽" ); //索引为1 names.add( "张飞" ); //索引为2 names.add( "刘备" ); //索引为3 names.add( "张飞" ); //索引为4 System.out.println(names.indexOf( "刘备" )); System.out.println(names.lastIndexOf( "刘备" )); System.out.println(names.indexOf( "张飞" )); System.out.println(names.lastIndexOf( "张飞" )); |
5.根据元素索引位置进行的判断;
1
2
3
4
5
6
7
|
if (names.indexOf( "刘备" )== 0 ) { System.out.println( "刘备在这里" ); } else if (names.lastIndexOf( "刘备" )== 3 ) { System.out.println( "刘备在那里" ); } else { System.out.println( "刘备到底在哪里?" ); } |
6.利用list中索引位置重新生成一个新的list(截取集合);
方法: .subList(fromIndex, toIndex); .size() ; 该方法得到list中的元素数的和
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
List<String> phone= new ArrayList<>(); phone.add( "三星" ); //索引为0 phone.add( "苹果" ); //索引为1 phone.add( "锤子" ); //索引为2 phone.add( "华为" ); //索引为3 phone.add( "小米" ); //索引为4 //原list进行遍历 for (String pho:phone){ System.out.println(pho); } //生成新list phone=phone.subList( 1 , 4 ); //.subList(fromIndex, toIndex) //利用索引1-4的对象重新生成一个list,但是不包含索引为4的元素,4-1=3 for ( int i = 0 ; i < phone.size(); i++) { // phone.size() 该方法得到list中的元素数的和 System.out.println( "新的list包含的元素是" +phone.get(i)); } |
7.对比两个list中的所有元素;
//两个相等对象的equals方法一定为true, 但两个hashcode相等的对象不一定是相等的对象
1
2
3
4
5
6
7
8
9
10
11
|
//1.<br>if (person.equals(fruits)) { System.out.println( "两个list中的所有元素相同" ); } else { System.out.println( "两个list中的所有元素不一样" ); } //2. if (person.hashCode()==fruits.hashCode()) { System.out.println( "我们相同" ); } else { System.out.println( "我们不一样" ); } |
8.判断list是否为空;
//空则返回true,非空则返回false
1
2
3
4
5
|
if (person.isEmpty()) { System.out.println( "空的" ); } else { System.out.println( "不是空的" ); } |
9.返回Iterator集合对象;
1
|
System.out.println( "返回Iterator集合对象:" +person.iterator()); |
1+0.将集合转换为字符串;
1
2
3
|
String liString= "" ; liString=person.toString(); System.out.println( "将集合转换为字符串:" +liString); |
11.将集合转换为数组;
1
|
System.out.println( "将集合转换为数组:" +person.toArray()); |
12.集合类型转换;
1
2
3
4
5
6
7
8
9
10
|
//1.默认类型 List<Object> listsStrings= new ArrayList<>(); for ( int i = 0 ; i < person.size(); i++) { listsStrings.add(person.get(i)); } //2.指定类型 List<StringBuffer> lst= new ArrayList<>(); for (String string:person){ lst.add(StringBuffer(string)); } |
13.去重复;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
List<String> lst1= new ArrayList<>(); lst1.add( "aa" ); lst1.add( "dd" ); lst1.add( "ss" ); lst1.add( "aa" ); lst1.add( "ss" ); //方法 1. for ( int i = 0 ; i <lst1.size()- 1 ; i++) { for ( int j = lst1.size()- 1 ; j >i; j--) { if (lst1.get(j).equals(lst1.get(i))) { lst1.remove(j); } } } System.out.println(lst1); //方法 2. List<String> lst2= new ArrayList<>(); for (String s:lst1) { if (Collections.frequency(lst2, s)< 1 ) { lst2.add(s); } } System.out.println(lst2); |
附完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
package MyTest01; import java.util.ArrayList; import java.util.List; public class ListTest01 { public static void main(String[] args) { //list中添加,获取,删除元素 List<String> person= new ArrayList<>(); person.add( "jackie" ); //索引为0 //.add(e) person.add( "peter" ); //索引为1 person.add( "annie" ); //索引为2 person.add( "martin" ); //索引为3 person.add( "marry" ); //索引为4 person.remove( 3 ); //.remove(index) person.remove( "marry" ); //.remove(Object o) String per= "" ; per=person.get( 1 ); System.out.println(per); ////.get(index) for ( int i = 0 ; i < person.size(); i++) { System.out.println(person.get(i)); //.get(index) } //list总是否包含某个元素 List<String> fruits= new ArrayList<>(); fruits.add( "苹果" ); fruits.add( "香蕉" ); fruits.add( "桃子" ); //for循环遍历list for ( int i = 0 ; i < fruits.size(); i++) { System.out.println(fruits.get(i)); } String appleString= "苹果" ; //true or false System.out.println( "fruits中是否包含苹果:" +fruits.contains(appleString)); if (fruits.contains(appleString)) { System.out.println( "我喜欢吃苹果" ); } else { System.out.println( "我不开心" ); } //list中根据索引将元素数值改变(替换) String a= "白龙马" , b= "沙和尚" , c= "八戒" , d= "唐僧" , e= "悟空" ; List<String> people= new ArrayList<>(); people.add(a); people.add(b); people.add(c); people.set( 0 , d); //.set(index, element) //将d唐僧放到list中索引为0的位置,替换a白龙马 people.add( 1 , e); //.add(index, element); //将e悟空放到list中索引为1的位置,原来位置的b沙和尚后移一位 //增强for循环遍历list for (String str:people){ System.out.println(str); } //list中查看(判断)元素的索引 List<String> names= new ArrayList<>(); names.add( "刘备" ); //索引为0 names.add( "关羽" ); //索引为1 names.add( "张飞" ); //索引为2 names.add( "刘备" ); //索引为3 names.add( "张飞" ); //索引为4 System.out.println(names.indexOf( "刘备" )); System.out.println(names.lastIndexOf( "刘备" )); System.out.println(names.indexOf( "张飞" )); System.out.println(names.lastIndexOf( "张飞" )); //根据元素索引位置进行的判断 if (names.indexOf( "刘备" )== 0 ) { System.out.println( "刘备在这里" ); } else if (names.lastIndexOf( "刘备" )== 3 ) { System.out.println( "刘备在那里" ); } else { System.out.println( "刘备到底在哪里?" ); } //利用list中索引位置重新生成一个新的list(截取集合) List<String> phone= new ArrayList<>(); phone.add( "三星" ); //索引为0 phone.add( "苹果" ); //索引为1 phone.add( "锤子" ); //索引为2 phone.add( "华为" ); //索引为3 phone.add( "小米" ); //索引为4 //原list进行遍历 for (String pho:phone){ System.out.println(pho); } //生成新list phone=phone.subList( 1 , 4 ); //.subList(fromIndex, toIndex) //利用索引1-4的对象重新生成一个list,但是不包含索引为4的元素,4-1=3 for ( int i = 0 ; i < phone.size(); i++) { // phone.size() 该方法得到list中的元素数的和 System.out.println( "新的list包含的元素是" +phone.get(i)); } //对比两个list中的所有元素 //两个相等对象的equals方法一定为true, 但两个hashcode相等的对象不一定是相等的对象 if (person.equals(fruits)) { System.out.println( "两个list中的所有元素相同" ); } else { System.out.println( "两个list中的所有元素不一样" ); } if (person.hashCode()==fruits.hashCode()) { System.out.println( "我们相同" ); } else { System.out.println( "我们不一样" ); } //判断list是否为空 //空则返回true,非空则返回false if (person.isEmpty()) { System.out.println( "空的" ); } else { System.out.println( "不是空的" ); } //返回Iterator集合对象 System.out.println( "返回Iterator集合对象:" +person.iterator()); //将集合转换为字符串 String liString= "" ; liString=person.toString(); System.out.println( "将集合转换为字符串:" +liString); //将集合转换为数组,默认类型 System.out.println( "将集合转换为数组:" +person.toArray()); ////将集合转换为指定类型(友好的处理) //1.默认类型 List<Object> listsStrings= new ArrayList<>(); for ( int i = 0 ; i < person.size(); i++) { listsStrings.add(person.get(i)); } //2.指定类型 List<StringBuffer> lst= new ArrayList<>(); for (String string:person){ lst.add(StringBuffer(string)); } } private static StringBuffer StringBuffer(String string) { return null ; } } |
Java中对List集合的常用操作(转载)的更多相关文章
- Java中对List集合的常用操作
目录: list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引 ...
- Java中对List集合的常用操作(转)
list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引位置重新 ...
- Java中对Array数组的常用操作
目录: 声明数组: 初始化数组: 查看数组长度: 遍历数组: int数组转成string数组: 从array中创建arraylist: 数组中是否包含某一个值: 将数组转成set集合: 将数组转成li ...
- Java中数组和集合的foreach操作编译后究竟是啥
今天和同事在关于foreach编译后是for循环还是迭代器有了不同意见,特做了个Demo,了解一下. 是啥自己来看吧! public class Demo { public static void m ...
- Java中如何克隆集合——ArrayList和HashSet深拷贝
编程人员经常误用各个集合类提供的拷贝构造函数作为克隆List,Set,ArrayList,HashSet或者其他集合实现的方法.需要记住的是,Java集合的拷贝构造函数只提供浅拷贝而不是深拷贝,这意味 ...
- 【集合】Java中的具体集合(一)
Java中不止提供了集合框架中的接口,还提供了许多具体的实现. Java中的具体集合 集合类型 描述 ArrayList 一种可以动态增长和缩减的索引序列 LinkedList 一种可以在任何位置进行 ...
- Java基础-Java中23种设计模式之常用的设计模式
Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...
- [PY3]——内置数据结构(6)——集合及其常用操作
集合及其常用操作Xmind图 集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...
- Redis集合的常用操作指令
Redis集合的常用操作指令 Sets常用操作指令 SADD 将指定的元素添加到集合.如果集合中存在该元素,则忽略. 如果集合不存在,会先创建一个集合然后在添加元素. 127.0.0.1:6379&g ...
随机推荐
- SQL Prompt几个快捷键
推荐一个小插件,SQL Prompt,配合Microsoft SQL Server Management Studio,使用起来非常方便,同时再加上以下几个快捷键: (1)ctrl+5或F5,运行代码 ...
- mysql string types ---- mysql 字符类型详解
一.mysql 中包涵的字符类型: [national] char [(m)] [character set charset_name] [collate collation_name] [natio ...
- 5.1 Zend_Log_Writer
22.2.2. 写入到数据库 22.2.3. 踩熄Writer 22.2.4. 測试 Mock 22.2.5. 组合Writers
- 验分享:CSS浮动(float,clear)通俗讲解
经验分享:CSS浮动(float,clear)通俗讲解 很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能力差,也可能是没能遇到一篇通俗的教程. 前些天小菜终于搞懂了浮动的基本原理,迫不 ...
- web.py+fastcgi+nginx 502错误解决
用web.py照着官网在服务器上搭好了后台.这次很奇怪地出现了一个Nginx 502 Bad Gateway的错误. 执行上面的kill `pgrep -f "python /path/to ...
- jquery 获取字符串中的数字
str_num = 'abc123' num = parseInt(str_num.replace(/[^0-9]/ig,"")); alert(num);
- python操作word(改课文格式)【最终版】
python操作word的一些方法,前面写了一些感悟,有点跑题,改了下题目,方便能搜索到.心急的可以直接拉到最后看代码,我都加了比较详细的注释. 从8.3号早上9点,到8.8号下午5点半下班,终于把这 ...
- NSURLErrorDomain Code=-999(转)
原文:http://www.henishuo.com/nsurlerrordomain-code-999/ 前言 今天有一个线上bug,是分配给提供H5的团队的,但是后台查不出来原因.于是让前端iOS ...
- C++的历史与现状
在31年前(1979年),一名刚获得博士学位的研究员,为了开发一个软件项目发明了一门新编程语言,该研究员名为Bjarne Stroustrup,该门语言则命名为——C with classes,四年后 ...
- PostgreSQL 配置远程访问
配置远 程连接PostgreSQL数据库的步骤很简单,只需要修改data目录下的pg_hba.conf和postgresql.conf. pg_hba.conf:配置对数据库的访问权限, postgr ...