**
关于java中List使用详细栗子
**
package org.znzz.practice;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import java.util.ArrayList;import java.util.Arrays;
import java.util.Collections;import java.util.List;
public class UseOfList {
public static void main(String[] args) {
// 向List对象中添加元素
List<String> person = new ArrayList<>();
person.add("jack");
person.add("peter");
person.add("flank");
person.add("Aries");
person.add("Tom");
person.remove("flank");
System.out.println(person.get(2));
// 通过遍历对象person对象获得元素
for (String s : person) {
System.out.println("对象是:" + s);
}
System.out.println("------------");
// list是否包含某个元素
List<String> fruits = new ArrayList<>();
fruits.add("苹果");
fruits.add("香蕉");
fruits.add("桃子");
// 遍历对象
/*
for (String f : fruits) {
System.out.println("对象是:" + f);
}*/
for (int i = 0; i < fruits.size(); i++) {
System.out.println("对象有:" + fruits.get(0));//通过下表取值
}
System.out.println("-------------");
System.out.println("fruits中是否包含苹果:" + fruits.contains("苹果"));
System.out.println("fruits中是否包含橘子:" + fruits.contains("橘子"));
if (fruits.contains("苹果")) {
System.out.println("I want to eat apple!!!");
} else {
System.out.println("I am not happy!!!");
}
System.out.println("------------");
// 根据索引值将元素数值改变
String a = "孙悟空", b = "唐僧", c = "猪八戒", d = "沙悟净", e = "白龙马";
List<String> people = new ArrayList<>();
people.add(a);
people.add(b);
people.add(c);// System.out.println(people); people.set(0, d); people.set(1, e);// 增强for循环遍历list for (String p : people) { System.out.println("对象是:" + p); } System.out.println("-------------");// list中查看判断元素的索引 List<String> names = new ArrayList<>(); names.add("刘备"); names.add("关羽"); names.add("张飞"); names.add("刘备"); names.add("张飞"); System.out.println(names); System.out.println(names.indexOf("刘备")); System.out.println(names.indexOf("张飞")); System.out.println(names.lastIndexOf("刘备")); System.out.println(names.lastIndexOf("张飞"));// 根据元素索引判断位置 if (names.indexOf("刘备") == 2) { System.out.println("刘备在此"); } else if (names.lastIndexOf("刘备") == 3) { System.out.println("刘备在远方"); } else { System.out.println("刘备在哪里呀!!!"); } System.out.println("-----------");// 利用list中索引位置重新生成一个新的list List<String> phone = new ArrayList<>(); phone.add("三星"); phone.add("小米"); phone.add("ViVO"); phone.add("华为"); phone.add("苹果"); for (String pho : phone) { System.out.print(pho + " "); } System.out.println(); phone = phone.subList(1, 4); System.out.println(phone); System.out.println("------------");// 对比两个list中所有元素 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("我们不一样"); System.out.println(person.hashCode()); System.out.println(fruits.hashCode()); }// 判断是否为空 if (people.isEmpty()) { System.out.println("集合为空"); } else { System.out.println("集合不为空"); } System.out.println("-------------");// 返回iterator集合对象 System.out.println(fruits.iterator()); System.out.println(person.iterator());// 将集合转换成字符串 String listr = ""; listr = person.toString(); System.out.println("集合转换成字符串为" + listr);// 集合转换成数组 System.out.println("集合转换成数组为:" + Arrays.toString(person.toArray())); System.out.println("-----------");// 集合类型转换// 1.默认类型 List<Object> listStrings = new ArrayList<>(); for (int i = 0; i < person.size(); i++) { listStrings.add(person.get(i)); } System.out.println(listStrings);// 2.指定类型 List<StringBuffer> lst = new ArrayList<>(); for (String string : person) { lst.add(new StringBuffer(string)); } System.out.println(lst); System.out.println("-----------");// 去重复 List<String> lst1 = new ArrayList<>(); lst1.add("aa"); lst1.add("dd"); lst1.add("ss"); lst1.add("aa"); lst1.add("ss");//方法1 List<String> lst2 = new ArrayList<>(); for (String s : lst1) { if (Collections.frequency(lst2, s) < 1) { lst2.add(s); } } System.out.println(lst2);//方法 2 List<String> lst3 = new ArrayList<>(); lst3.add("qq"); lst3.add("dd"); lst3.add("qq"); lst3.add("aa"); lst3.add("dd"); for (int i=0;i<lst3.size()-1;i++){ for (int j=i+1;j<lst3.size();j++){ if (lst3.get(i).equals(lst3.get(j))){ lst3.remove(lst3.get(j)); } } } System.out.println(lst3); }
}
}