程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA容器小結

JAVA容器小結

編輯:關於JAVA

JAVA中的容器提供了非常完善的方法來保存對象,你可以使用這些工具來解決大數據量的問題。下面是筆者在開發中用到過的一些容器總結如此。

1 ArrayList

使用ArrayList非常簡單:創建一個實例,用add()插入對象,然後用get()訪問這些對象,此時需要索引,就象數組一樣,但是不需要方括號,ArrayList還有size()方法,從而可以知道ArrayList的大小,也可以避免因為越界而引發錯誤。另外,ArrayList長於隨機訪問元素,但是在List的中間插入和移處元素時較慢。下面是ArrayList的例子(來自thinking in java):

/**//*
* @(#)AppleAndOrangesWithGenerics.java  1.0 May 17, 2008
* @author:Administrator
* Copyright 2008 CSS WEB Microsystems, Inc. All rights reserved.
* CSS WEB ROOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package holding;
import java.util.ArrayList;
public class AppleAndOrangesWithGenerics ...{
public static void main(String[] args) ...{
ArrayList<Apple> apples = new ArrayList<Apple>();
for(int i = 0; i < 3; i++) ...{
apples.add(new Apple());
}
for(int i = 0; i < apples.size(); i++)
System.out.println(((Apple)apples.get(i)).id());
for(Apple c:apples)
System.out.println(c.id()+"-");
}
}
/**//*
* @(#)ListFeatures.java  1.0 May 17, 2008
* @author:Administrator
* Copyright 2008 CSS WEB Microsystems, Inc. All rights reserved.
* CSS WEB ROOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package holding;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import typeinfo.pets.*;
public class ListFeatures ...{
public static void main(String[] args) ...{
Random rand = new Random(47);
List<Pet> pets = Pets.arrayList(7);
System.out.print("1: " + pets);
System.out.println();
Hamster h = new Hamster();
pets.add(h);
System.out.print("2: " + pets);
System.out.println();
System.out.print("3: " + pets.contains(h));
System.out.println();
pets.remove(h);
Pet p = pets.get(2);
System.out.print("4: " + pets.indexOf(p));
System.out.println();
Pet cymric = new Cymric();
System.out.print("5: " + pets.indexOf(cymric));
System.out.println();
System.out.print("6: " + pets.remove(cymric));
System.out.println();
System.out.print("7: " + pets.remove(p));
System.out.println();
System.out.print("8: " + pets);
System.out.println();
pets.add(3,new Mouse());
System.out.print("9: " + pets);
System.out.println();
List<Pet> sub = pets.subList(1,4);
System.out.print("sublist: " + sub);
System.out.println();
System.out.print("10: " + pets.containsAll(sub));
System.out.println();
Collections.sort(sub);
System.out.print("sorted sublist: " + sub);
System.out.println();
System.out.print("11: " + pets.containsAll(sub));
System.out.println();
Collections.shuffle(sub, rand);
System.out.print("shuffle sublist: " + sub);
System.out.println();
System.out.print("12: " + pets.containsAll(sub));
System.out.println();
List<Pet> copy = new ArrayList<Pet>(pets);
sub = Arrays.asList(pets.get(1),pets.get(4));
System.out.print("sub: " + sub);
System.out.println();
copy.retainAll(sub);
System.out.print("13: " + copy);
System.out.println();
copy = new ArrayList<Pet>(pets);
copy.remove(2);
System.out.print("14: " + copy);
System.out.println();
copy.removeAll(sub);
System.out.print("15: " + copy);
System.out.println();
copy.set(1, new Mouse());//Replace an element
System.out.print("16: " + copy);
System.out.println();
copy.addAll(2, sub);
System.out.print("17: " + copy);
System.out.println();
System.out.print("18: " + pets.isEmpty());
System.out.println();
pets.clear();
System.out.print("19: " + pets);
System.out.println();
System.out.print("20: " + pets.isEmpty());
System.out.println();
pets.addAll(Pets.arrayList(4));
System.out.print("21: " + pets);
System.out.println();
Object[] o = pets.toArray();
System.out.print("22: " + o[3]);
System.out.println();
Pet[] pa = pets.toArray(new Pet[0]);
System.out.print("23: " + pa[3].id());
System.out.println();
}
}

運行結果:

1: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug]
2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster]
3: true
4: 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
shuffle 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

2 LinkedList

LinkedList也像ArrayList一樣實現了基本的List接口,但是它執行某些操作(比如插入、刪除)時要比ArrayList更加的高效,但是在隨機訪問操作方面要遜色一些。此外,LinkedList還添加了可以使其用作棧、隊列或雙端隊列的方法。下面是關於LinkedList使用的例子:

/**//*
* @(#)LinkedListFeatures.java  1.0 May 18, 2008
* @author:Administrator
* Copyright 2008 CSS WEB Microsystems, Inc. All rights reserved.
* CSS WEB ROOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package holding;
import java.util.LinkedList;
import static net.mindview.util.Print.*;
import typeinfo.pets.Hamster;
import typeinfo.pets.Pet;
import typeinfo.pets.Pets;
import typeinfo.pets.Rat;
public class LinkedListFeatures ...{
public static void main(String[] args) ...{
LinkedList<Pet> pets = new LinkedList<Pet>(Pets.arrayList(5));
print(pets);
print("pets.getFirst(): " + pets.getFirst());
print("pets.element(): " + pets.element());
print("pets.peek(): " + pets.peek());
print("pets.remove(): " + pets.remove());
print("pets.removeFirst(): " + pets.removeFirst());
print("pets.poll(): " + pets.poll());
print(pets);
pets.addFirst(new Rat());
print("After addFirst(): " + pets);
pets.offer(Pets.randomPet());
print("After offer(): " + pets);
pets.add(Pets.randomPet());
print("After add(): " + pets);
pets.addLast(new Hamster());
print("After addLast(): " + pets);
print("pets.removeLast(): " + pets.removeLast());
}
}

運行結果:

[Rat, Manx, Cymric, Mutt, Pug]
pets.getFirst(): Rat
pets.element(): Rat
pets.peek(): Rat
pets.remove(): Rat
pets.removeFirst(): Manx
pets.poll(): Cymric
[Mutt, Pug]
After addFirst(): [Rat, Mutt, Pug]
After offer(): [Rat, Mutt, Pug, Cymric]
After add(): [Rat, Mutt, Pug, Cymric, Pug]
After addLast(): [Rat, Mutt, Pug, Cymric, Pug, Hamster]
pets.removeLast(): Hamster

List的排序和查詢:List排序和查詢所使用的方法與對象數組所使用的相應方法有相同的名字和語法,只是用Collections的static方法代替Arrays的方法而已。例子如下:

/**//*
* @(#)ListSortSearch.java  1.0 May 18, 2008
* @author:Administrator
* Copyright 2008 CSS WEB Microsystems, Inc. All rights reserved.
* CSS WEB ROOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package containers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import containers.Utilities;
import static net.mindview.util.Print.*;
public class ListSortSearch ...{
public static void main(String[] args) ...{
List<String> list = new ArrayList<String>(Utilities.list);
list.addAll(Utilities.list);
print(list);
Collections.shuffle(list,new Random(47));
print("shuffled: " + list);
ListIterator<String> it = list.listIterator(10);
while(it.hasNext()) ...{
it.next();
it.remove();
}
print("Trimmed: " + list);
Collections.sort(list);
print("sorted: " + list);
String key = list.get(7);
int index = Collections.binarySearch(list, key);
print("Location of " + key + " is " + index +
", list.get(" + index + ") = " + list.get(index));
Collections.sort(list,String.CASE_INSENSITIVE_ORDER);
print("Case-insensitive sorted: " + list);
key = list.get(7);
index = Collections.binarySearch(list, key, String.CASE_INSENSITIVE_ORDER);
print("Location of " + key + " is " + index +
", list.get(" + index + ") = " + list.get(index));
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved