List集合常用API
By
xyp-hf
Update date:
欢迎访问CSDN博客专栏CSDN专栏 Java全栈之路,Github主页。
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
| import java.util.LinkedList; import java.util.List;
public class TestList {
public static void main(String[] args) {
List l1 = new LinkedList();
boolean b1 = l1.add(new Integer(1)); System.out.println("b1 = " + b1); System.out.println("l1 = " + l1); b1 = l1.add(new Integer(2)); System.out.println("b1 = " + b1); System.out.println("l1 = " + l1);
l1.add(0, new String("one")); System.out.println("l1 = " + l1); l1.add(2, new String("two")); System.out.println("l1 = " + l1); l1.add(4, new Integer(3)); System.out.println("l1 = " + l1); System.out.println("---------------------------------------------");
List l2 = new LinkedList();
l2.add("three"); l2.add(4); System.out.println("l2 = " + l2); l1.addAll(l2); System.out.println("l1 = " + l1); System.out.println("l2 = " + l2); l1.addAll(0, l2); System.out.println("l1 = " + l1); System.out.println("l2 = " + l2); System.out.println("---------------------------------------------");
String str = (String) l1.set(0, 3); System.out.println("str = " + str); System.out.println("l1 = " + l1); str = (String) l1.set(2, 1); System.out.println("str = " + str); System.out.println("l1 = " + l1); Integer it1 = (Integer) l1.set(1, "four"); System.out.println("it1 = " + it1); System.out.println("l1 = " + l1); System.out.println("---------------------------------------------");
it1 = (Integer) l1.get(0); System.out.println("it1 = " + it1); System.out.println("l1 = " + l1); str = (String) l1.get(1); System.out.println("str = " + str); System.out.println("l1 = " + l1); System.out.println("---------------------------------------"); System.out.print("l1 = ["); for(int i = 0; i < l1.size(); i++){ if(i == l1.size()-1){ System.out.println(l1.get(i) + "]"); } else{ System.out.print(l1.get(i) + ", "); } } System.out.println(); System.out.println("---------------------------------------");
System.out.println("l1 = " + l1); System.out.println("---------------------------------------");
List l3 = l1.subList(3, 8); System.out.println("l3 = " + l3); System.out.println("l1 = " + l1); System.out.println("集合l3中删除的元素是:" + l3.remove(0)); System.out.println("l3 = " + l3); System.out.println("l1 = " + l1); }
}
|