1. 程式人生 > >持有物件:List介面的常用方法

持有物件:List介面的常用方法




/*
 *  2018年3月27日10:05:52
 *  程式碼位置:P223
 *  程式碼目的:
 *   說明List介面的常用方法和特徵。
 *  list的常用方法需要多多實踐才能靈活使用
 * */

package holding; //: holding/ListFeatures.java import typeinfo.pets.*; import java.util.*; import static net.mindview.util.Print.*; public class ListFeatures { public static void main(String[] args) { Random rand = new Random(47); List<Pet> pets = Pets.arrayList(7); print("1: " + pets); Hamster h = new Hamster(); pets.add(h); // Automatically resizes 追加到連結串列尾部 print("2: " + pets); print("3: " + pets.contains(h));//判斷包含關係 pets.remove(h); // Remove by object Pet p = pets.get(2); // 由索引獲取 print("4: " + p + " " + pets.indexOf(p)); Pet cymric = new Cymric(); /* * 返回此列表中第一次出現的指定元素的索引; * 如果此列表不包含該元素,則返回 -1。 * 更確切地講,返回滿足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引 i; * 如果沒有這樣的索引,則返回 -1。 引數: * */ print("5: " + pets.indexOf(cymric)); print("6: " + pets.remove(cymric)); // Must be the exact object: print("7: " + pets.remove(p)); print("8: " + pets); //在指定位置插入元素 pets.add(3, new Mouse()); // Insert at an index print("9: " + pets); /* * 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之間的部分檢視。 * (如果 fromIndex 和 toIndex 相等,則返回的列表為空)。 * 返回的列表由此列表支援,因此返回列表中的非結構性更改將反映在此列表中,反之亦然。 * 返回的列表支援此列表支援的所有可選列表操作。 * */ List<Pet> sub = pets.subList(1, 4); print("subList: " + sub); print("10: " + pets.containsAll(sub)); Collections.sort(sub); // In-place sort print("sorted subList: " + sub); // Order is not important in containsAll(): print("11: " + pets.containsAll(sub)); //使用指定的隨機源對指定列表進行置換。 Collections.shuffle(sub, rand); // Mix it up print("shuffled subList: " + sub); print("12: " + pets.containsAll(sub)); List<Pet> copy = new ArrayList<Pet>(pets); sub = Arrays.asList(pets.get(1), pets.get(4)); print("sub: " + sub); //取交集 copy.retainAll(sub); print("13: " + copy); copy = new ArrayList<Pet>(pets); // Get a fresh copy copy.remove(2); // Remove by index print("14: " + copy); copy.removeAll(sub); // Only removes exact objects print("15: " + copy); copy.set(1, new Mouse()); // Replace an element print("16: " + copy); copy.addAll(2, sub); // Insert a list in the middle print("17: " + copy); print("18: " + pets.isEmpty()); pets.clear(); // Remove all elements print("19: " + pets); print("20: " + pets.isEmpty()); pets.addAll(Pets.arrayList(4)); print("21: " + pets); Object[] o = pets.toArray(); print("22: " + o[3]); Pet[] pa = pets.toArray(new Pet[0]); print("23: " + pa[3].id()); } }

1: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug]
2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster]
3: true
4: Cymric 2
5: -1
6: false
7: true
8: [Rat, Manx, Mutt, Pug, Cymric, Pug]
9: [Rat, Manx, Mutt, Mouse, Pug, Cymric, Pug]
subList: [Manx, Mutt, Mouse]
10: true
sorted subList: [Manx, Mouse, Mutt]
11: true
shuffled subList: [Mouse, Manx, Mutt]
12: true
sub: [Mouse, Pug]
13: [Mouse, Pug]
14: [Rat, Mouse, Mutt, Pug, Cymric, Pug]
15: [Rat, Mutt, Cymric, Pug]
16: [Rat, Mouse, Cymric, Pug]
17: [Rat, Mouse, Mouse, Pug, Cymric, Pug]
18: false
19: []
20: true
21: [Manx, Cymric, Rat, EgyptianMau]
22: EgyptianMau
23: 14
*///:~