詳談Java的list類

1、list中添加,獲取,刪除元素:

添加方法是:.add(e);  
獲取方法是:.get(index);  
刪除方法是:.remove(index); 按照索引刪除;  
		  .remove(Object o); 按照元素內容刪除;

示例代碼:

	List<String> person = new ArrayList<>();
	person.add("張三"); // 索引爲0 //.add(e)
	person.add("李四"); // 索引爲1
	person.add("王五"); // 索引爲2
	person.add("宋六"); // 索引爲3
	person.add("錢七"); // 索引爲4

	person.remove(3); // .remove(index)
	person.remove("錢七"); // .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總是否包含某個元素
	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中根據索引將元素數值改變(替換):

示例代碼:

	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中查看(判斷)元素的索引:

示例代碼:

	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("張三") == 2) {
		System.out.println("張三不在這裏");
	} else {
		System.out.println("張三到底在哪裏?");
	}

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

示例代碼:

	List<String> phone = new ArrayList<>();
	phone.add("三星"); // 索引爲0
	phone.add("蘋果"); // 索引爲1
	phone.add("vivo"); // 索引爲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相等的對象不一定是相等的對象
	if (person.equals(fruits)) {
		System.out.println("兩個list中的所有元素相同");
	} else {
		System.out.println("兩個list中的所有元素不一樣");
	}

	if (person.hashCode() == fruits.hashCode()) {
		System.out.println("兩個list相同");
	} else {
		System.out.println("兩個list不同");
	}

8、.判斷list是否爲空:

示例代碼:

	// 空則返回true,非空則返回false
	if (person.isEmpty()) {
		System.out.println("list爲空的");
	} else {
		System.out.println("list不是空的");
	}

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中去重複:

示例代碼:

package com.gx.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test3 {
	public static void main(String[] args) {
		List<String> lst1 = new ArrayList<>();
		lst1.add("aaa");
		lst1.add("bbb");
		lst1.add("ccc");
		lst1.add("aaa");
		lst1.add("bbb");

		// 方法 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);
	}
}

附完整代碼:

package com.gx.demo;

import java.util.ArrayList;
import java.util.List;

public class Test4 {
	public static void main(String[] args) {

		System.out.println("-----------------------list中添加,獲取,刪除元素--------------------------");
		List<String> person = new ArrayList<>();
		person.add("張三"); // 索引爲0 //.add(e)
		person.add("李四"); // 索引爲1
		person.add("王五"); // 索引爲2
		person.add("宋六"); // 索引爲3
		person.add("錢七"); // 索引爲4

		person.remove(3); // .remove(index)
		person.remove("錢七"); // .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)
		}

		System.out.println("-----------------------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("沒有蘋果了");
		}

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

		System.out.println("-----------------------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("王五"));

		System.out.println("-----------------------根據元素索引位置進行的判斷--------------------------");
		if (names.indexOf("張三") == 0) {
			System.out.println("張三在這裏");
		} else if (names.lastIndexOf("張三") == 2) {
			System.out.println("張三不在這裏");
		} else {
			System.out.println("張三到底在哪裏?");
		}

		System.out.println("-----------------------利用list中索引位置重新生成一個新的list(截取集合)--------------------------");
		List<String> phone = new ArrayList<>();
		phone.add("三星"); // 索引爲0
		phone.add("蘋果"); // 索引爲1
		phone.add("vivo"); // 索引爲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));
		}

		System.out.println("-----------------------對比兩個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("兩個list相同");
		} else {
			System.out.println("兩個list不同");
		}

		System.out.println("-----------------------判斷list是否爲空--------------------------");
		// 空則返回true,非空則返回false
		if (person.isEmpty()) {
			System.out.println("list爲空的");
		} else {
			System.out.println("list不是空的");
		}

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

		System.out.println("-----------------------將集合轉換爲字符串--------------------------");
		String liString = "";
		liString = person.toString();
		System.out.println("將集合轉換爲字符串:" + liString);

		System.out.println("-----------------------將集合轉換爲數組,默認類型--------------------------");
		System.out.println("將集合轉換爲數組:" + person.toArray());

		System.out.println("-----------------------集合類型轉換--------------------------");
		// 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;
	}
}

輸出結果:

-----------------------list中添加,獲取,刪除元素--------------------------
李四
張三
李四
王五
-----------------------list總是否包含某個元素--------------------------
蘋果
香蕉
雪梨
fruits中是否包含蘋果:true
我喜歡喫蘋果
-----------------------list中根據索引將元素數值改變(替換)--------------------------
路人丁
路人戊
路人乙
路人丙
-----------------------list中查看(判斷)元素的索引--------------------------
0
3
2
4
-----------------------根據元素索引位置進行的判斷--------------------------
張三在這裏
-----------------------利用list中索引位置重新生成一個新的list(截取集合)--------------------------
三星
蘋果
vivo
華爲
小米
新的list包含的元素是:蘋果
新的list包含的元素是:vivo
新的list包含的元素是:華爲
-----------------------對比兩個list中的所有元素--------------------------
兩個list中的所有元素不一樣
兩個list不同
-----------------------判斷list是否爲空--------------------------
list不是空的
-----------------------返回Iterator集合對象--------------------------
返回Iterator集合對象:java.util.ArrayList$Itr@3a504f3c
-----------------------將集合轉換爲字符串--------------------------
將集合轉換爲字符串:[張三, 李四, 王五]
-----------------------將集合轉換爲數組,默認類型--------------------------
將集合轉換爲數組:[Ljava.lang.Object;@6e820a0c
-----------------------集合類型轉換--------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章