1.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)
}

2.list中是否包含某个元素

方法:.contains(Object o); 返回true或者false

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); 的不同

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()的不同

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.根据元素索引位置进行的判断

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中的元素数的和

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.<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

if (person.isEmpty()) {
System.out.println("空的");
}else {
System.out.println("不是空的");
}

9.返回Iterator集合对象

System.out.println("返回Iterator集合对象:"+person.iterator());

10.将集合转换为字符串

String liString="";
liString=person.toString();
System.out.println("将集合转换为字符串:"+liString);

11.将集合转换为数组

System.out.println("将集合转换为数组:"+person.toArray());

12.集合类型转换

//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.去重复

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. HDFS 与 GFS 的设计差异
  2. Mobile Safari调用本地App, 否则进入App Store下载
  3. 服务链(Service Chaining,or Service Function Chaining,SFC,功能服务链)
  4. Android常见控件— — —ProgressBar
  5. spring 标注
  6. 001 The Hello World In Csharp
  7. VI小技巧
  8. macos ssh host配置及免密登陆
  9. storm 事务和DRPC结合
  10. scala中的implict
  11. Keil MDK从未有过的详细使用讲解
  12. Python3学习笔记十五
  13. (3)编译安装lamp三部曲之php-技术流ken
  14. BurpSuit2.0专业版破解
  15. Redis——redis使用redis-dump,redis-load导出导入数据——【三】
  16. cdh部署
  17. 高级设计总监的设计方法论——5W1H需求分析法 KANO模型分析法
  18. vue项目webpack中Npm传递参数配置不同域名接口
  19. Nginx记录-Nginx配置
  20. Linq模型ObjectContext下查看Sql语句。

热门文章

  1. VBox虚拟机与主机(宿主)通讯原理以及socat(套接字猫)简单介绍
  2. HDU 3473
  3. Ubuntu下调整时区时间
  4. PPAPI插件的全屏切换处理
  5. the solution of CountNonDivisible by Codility
  6. hdu3592 World Exhibition --- 差分约束
  7. 逻辑回归 C++
  8. 98.Ext.form.Label组件的基本用法
  9. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity
  10. Winform 异步调用2 时间