List 列表常用操作
初始化列表
package org.onepiece; import java.lang.String; import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { //1 List<String> emptyList = new ArrayList<>(); System.out.println(emptyList.getClass().getName());//java.util.ArrayList System.out.println(emptyList);//[] //2 String[] array = {"AA", "BB", "cc", "DD", "EE"}; List<String> list = new ArrayList<>(Arrays.asList(array)); System.out.println(list.getClass().getName());//java.util.ArrayList System.out.println(list);//[AA, BB, cc, DD, EE] //3 List<String> newList = new ArrayList<>(Arrays.asList("AA", "BB", "cc", "DD", "EE")); System.out.println(newList.getClass().getName());//java.util.ArrayList System.out.println(newList);//[AA, BB, cc, DD, EE] } }
//比较 -> 引用地址 System.out.println(list == newList); //false //比较 -> 值 System.out.println(list.equals(newList));//true
新增(add)、删除(remove)、随机访问(get) 元素
package org.onepiece; import java.lang.String; import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(); System.out.println(list);//[] //添加元素 list.add("one"); list.add("two"); list.add("three"); list.add("four"); System.out.println(list);//[one, two, three, four] //删除元素 list.remove("five"); System.out.println(list);//[one, two, three, four] list.remove("four"); System.out.println(list);//[one, two, three] //删除指定位置的元素 list.remove(1); System.out.println(list);//[one, three] //访问随机元素 String str = list.get(0); System.out.println(str);//one } }
列表判断操作方法
package org.onepiece; import java.lang.String; import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(); System.out.println(list);//[] //列表是否为空 boolean isEmpty = list.isEmpty(); System.out.println(isEmpty);//true //列表大小 int size = list.size(); System.out.println(size);//0 System.out.println(list.contains("one"));//false System.out.println(list.contains("123"));//false //添加元素 list.add("one"); list.add("two"); list.add("three"); list.add("123"); isEmpty = list.isEmpty(); System.out.println(isEmpty);//false size = list.size(); System.out.println(size);//4 System.out.println(list.contains("one"));//true System.out.println(list.contains("123"));//true System.out.println(list.contains("one123"));//false //转化成数组 String[] array = list.toArray(new String[list.size()]); System.out.println(array.length);//4 System.out.println(Arrays.toString(array));//[one, two, three, 123] } }
遍历列表
package org.onepiece; import java.lang.String; import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three")); System.out.println(list);//[one, two, three] //1:for for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } //one two three
//2:foreach for (String str : list) { System.out.print(str + " "); } //one two three
//3:lambda表达式 list.forEach(s -> { System.out.print(s + " "); }); //one two three } }
//备注:
//如果 list.size()==0,则它不会进入循环遍历的方法块里面 { }
列表其它操作
package org.onepiece; import java.lang.String; import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five")); System.out.println(list);//[one, two, three, four, five]
//removeIf:用于删除满足某个条件的元素 //删掉含有"o"字符的字符串 //[one,two,four] list.removeIf(s -> { return s.contains("o"); }); System.out.println(list);//[three, five] //删掉字符串长度大于的字符串 //[three] list.removeIf(s -> { return s.length() > 4; }); System.out.println(list);//[five] //删掉含有"f"字符的字符串 //[five] list.removeIf(s -> s.contains("f")); System.out.println(list);//[]
list.add("AA"); System.out.println(list);//[AA] //addAll:将other1集合中的所有元素添加到list列表 //并集 Collection<String> other1 = Arrays.asList("AA", "BB", "CC"); list.addAll(other1); System.out.println(list);//[AA, AA, BB, CC] //removeAll:list列表中删除other2集合中存在的元素 //差集 Collection<String> other2 = Arrays.asList("AA", "BB"); list.removeAll(other2); System.out.println(list);//[CC] list.add("dd"); System.out.println(list);//[CC, dd] //retainAll:list列表中删除所有与other3集合中的不同元素 //交集 Collection<String> other3 = Arrays.asList("CC", "dd", "efg"); list.retainAll(other3); System.out.println(list);//[CC, dd] //清空所有元素 list.clear(); System.out.println(list);//[] } }

浙公网安备 33010602011771号