java的list类
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自定义注解类
一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...
- 基础知识(05) -- Java中的类
Java中的类 1.类的概念 2.类中的封装 3.对象的三大特征 4.对象状态 5.类与类之间的关系 ------------------------------------------------- ...
- java中Inetaddress类
InetAddress类 InetAddress类用来封装我们前面讨论的数字式的IP地址和该地址的域名. 你通过一个IP主机名与这个类发生作用,IP主机名比它的IP地址用起来更简便更容易理解. Ine ...
- Java集合---Array类源码解析
Java集合---Array类源码解析 ---转自:牛奶.不加糖 一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Prim ...
- 浅析Java.lang.ProcessBuilder类
最近由于工作需要把用户配置的Hive命令在Linux环境下执行,专门做了一个用户管理界面特地研究了这个不经常用得ProcessBuilder类.所以把自己的学习的资料总结一下. 一.概述 P ...
- 浅析Java.lang.Process类
一.概述 Process类是一个抽象类(所有的方法均是抽象的),封装了一个进程(即一个执行程序). Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的 ...
- 浅析Java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- java单例类/
java单例类 一个类只能创建一个实例,那么这个类就是一个单例类 可以重写toString方法 输出想要输出的内容 可以重写equcal来比较想要比较的内容是否相等 对于final修饰的成员变量 一 ...
- JAVA中的类和接口
1.类: 类是具有相同属性和方法的一组对象的集合,它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和方法两个主要部分.在面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属 ...
- 两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz
1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 import java.util.TimerTask; public class EmailReportT ...
随机推荐
- [Android]异常1-duplicate files during packaging of
异常原因: 可能一>项目存在重复资源文件 可能二>Android Studio与源代码Android Studio不一致 解决方法有: 解决一>查找重复资源,删除或者修改文件 解决二 ...
- PAT甲级1016Phone Bills
#include<iostream> #include<cstdio> #include<cstdlib> #include<vector> #incl ...
- 【译】x86程序员手册00 - 翻译起因
从上一次学习MIT的操作系统课程又过去了一年.上次学习并没有坚持下去.想来虽有种种原因,其还在自身无法坚持罢了.故此次再鼓起勇气重新学习,发现课程都已由2014改版为2016了.但大部分内容并没有改变 ...
- Android中Application类总结
本文出处: 炎之铠csdn博客:http://blog.csdn.net/totond 炎之铠邮箱:yanzhikai_yjk@qq.com 本文原创,转载请注明本出处! 前言 最近的开发中经常使用到 ...
- ionic下拉多项选择
1.npm install ion-multi-picker --save 2.引入 import { MultiPickerModule } from 'ion-multi-picker'; imp ...
- CAD直接打印,不出现打印对话框(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_Print 直接打印,不出现打印对话框,详细说明如下: 参数 说明 double ptLBx 打印的范围左下角x double ...
- 怎么选择最适合自己的Linux培训机构?
Linux培训已经成为入门Linux的一个重要途径,它的优势在于学习知识的系统性.快速性和实用性.Linux培训毕业的学员大多数拥有较强的实战动手能力,能够较快上手,更符合企业需求. 不过,大部分同学 ...
- 面试:A
分析 System.Collections.Generic.List<T> 的 Remove<T> 方法和 Clear 方法的实现细节(不允许使用“移除”“清除”这种概念模糊的 ...
- Python基础-奇偶判断调用函数
编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数 1/1+1/3+...+1/n. 首先写一个n为偶数的函数: def peven(n): s = 0 ...
- 优雅的退出/关闭/重启gunicorn进程
在工作中,会发现gunicorn启动的web服务,无论怎么使用kill -9 进程号都是无法杀死gunicorn,经过我一番百度和谷歌,发现想要删除gunicorn进程其实很简单. 第一步获取Guni ...