一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java中對List集合的常用操作詳解

Java中對List集合的常用操作詳解

2020-05-25 11:33jingxian JAVA教程

下面小編就為大家帶來一篇Java中對List集合的常用操作詳解。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

目錄:

1.list中添加,獲取,刪除元素;

2.list中是否包含某個元素;

3.list中根據索引將元素數值改變(替換);

4.list中查看(判斷)元素的索引;

5.根據元素索引位置進行的判斷;

6.利用list中索引位置重新生成一個新的list(截取集合);

7.對比兩個list中的所有元素;

8.判斷list是否為空;

9.返回Iterator集合對象;

10.將集合轉換為字符串;

11.將集合轉換為數組;

12.集合類型轉換;

備注:內容中代碼具有關聯性。

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));
}

附完整代碼:

?
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集合的常用操作詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本大片免a费观看在线 | 久久re热在线视频精6 | 成年人视频免费在线观看 | 男人捅女人动漫 | 成人网18免费网站 | 女人c交zzzooo在线观看 | 久久久久青草大香线综合精品 | 99热在线精品播放 | 亚洲国产精品第一页 | 精品区卡一卡2卡三免费 | 女人扒开下面让男人桶爽视频 | 国产亚洲欧美成人久久片 | 亚洲国产高清一区二区三区 | 五月天淫 | 春宵福利网站在线观看 | 国产成人一区二区三区 | 欧美视频一区二区三区四区 | 亚洲AV无码国产精品色在线看 | 超强台风免费观看完整版视频 | 掰开逼操| 我的妹妹最近有点怪免费播放 | 我年轻漂亮的继坶2中字在线播放 | 日朝欧美亚洲精品 | 青草青草视频2免费观看 | 男人的天堂在线观看入口 | 国内体内she精视频免费 | 鬼惨笑小说 | 毛片一级毛片 | 免费亚洲一区 | 免费在线视频观看 | 男女男精品视频免费观看 | 特黄特黄一级高清免费大片 | 337p大尺度啪啪人体午夜2020 | 国产精品毛片高清在线完整版 | 国产91精选学生在线观看 | 亚洲精品国产成人 | 91欧美秘密入口 | 男女交性特一级 | 国色天香 社区视频 | 嫩草视频在线观看免费 | 日本不卡1卡2卡三卡网站二百 |