详谈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
-----------------------集合类型转换--------------------------

猜你喜欢

转载自blog.csdn.net/weixin_44563573/article/details/103766269