所有的东东都是Collection(总结口)
Array有多少元素是确定的,比如足球队上场的队员有11名,是固定的,就用array。
ArrayList是不固定的,比如用sql查询数据库,不知道有多少记录返回,用arraylist.
Enumeration是用来一个一个列举Collection的元素的,但java2后被Iterator替代。
Hashtable用在比如你想查中国队的10号是谁,首先put(new Interger(10),new String(“海东”)),再String name=get(new Interger(10));对于简单的key--value对来说,hashtable很有用,建议hashmap.

解决方案 »

  1.   

    take a look at "think in java", it's explained in detail there
      

  2.   

    還有Vector,Hashmap都用在什么地方啊?
      

  3.   

    Vector和ArrayList差不多
    Hashtable和Hashmap也差不多
    他们的区别就是前者的方法有考虑同步
    后者没有
    所以如果可以确定不需要同步的话
    用后者效率更高就我记得的是这些
      

  4.   

    XKP(低等下人):
         可以给个具体的列子吗?
      

  5.   

    我觉得在多线程以及数据库的更新
    这两方面就有可能要用到同步的方法比如说有一个静态的集合(姑且这么叫)
    在多线程里面同时有很多要更改这个集合(比如添加数据)
    而且对于这些数据的添加顺序有一定要求的话
    就应该用Vector。我的理解是这样的
    还请其他人指教
      

  6.   

    我到是发现了一个问题
    :
    有一次我用时,
    把一个数组放在一个HashMap中
    当然是一个二维数组...
    打印出来发现,原来在数组中有序的东西,
    出来全没有顺序了...
    后来用TreeMap才解决了问题..
      

  7.   

    范例
    collections 是一个「家常便饭」的主题,实在难以设计出令人兴奋的范例。下面的范例程序用来读取文字文件、剖析文字、计算出现频率、排序、并将结果写到另一个档案。透过此范例,你应该 就能体会要如何使用 collection: import java.io.*;
    import java.util.*; public class WordSort {
      public static void main(String[] args) throws IOException {
        // Get the command-line arguments.
        if (args.length < 2) {
          System.out.println("Usage: WordSort inputfile outputfile");
          return;
        }
        String inputfile = args[0];
        String outputfile = args[1];
            // Create the word map. Each key is a word
        //   and each value is an Integer that represents
        //   the number of times the word occurs in the
        //   input file.
        Map map = new HashMap();
            // Read every line of the input file.
        BufferedReader in = new BufferedReader(new FileReader(inputfile));
        String line;
        while ((line = in.readLine()) != null) {
          // Examine each word on the line.
          StringTokenizer st = new StringTokenizer(line);
          while (st.hasMoreTokens()) {
            String word = st.nextToken();
            Object o = map.get(word);
            // If there's no entry for this word, add one.
            if (o == null) map.put(word, new Integer(1));
            // Otherwise, increment the count for this word.
            else {
              Integer count = (Integer)o;
              map.put(word, new Integer(count.intValue() + 1));
            }
          }
        }
        in.close();
            // Get the map's keys and sort them.
        List keys = new ArrayList(map.keySet());
        Collections.sort(keys);     // Now write the results to the output file.
        PrintWriter out = new PrintWriter(new FileWriter(outputfile));
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
          Object key = iterator.next();
          out.println(key + " : " + map.get(key));
        }
        out.close();
      }
    } 如果输入档 Ian Moore.txt 的内容如下:     Well it was my love that kept you going
        Kept you strong enough to fall
        And it was my heart you were breaking
        When he hurt your pride
        So how does it feel
        How does it feel
        How does it feel
        How does it feel 你可以用下面的方式来执行:     java WordSort "Ian Moore.txt" count.txt 执行完后的结果存在 count.txt 档中,内容如下:     And : 1
        How : 3
        Kept : 1
        So : 1
        Well : 1
        When : 1
        breaking : 1
        does : 4
        enough : 1
        fall : 1
        feel : 4
        going : 1
        he : 1
        heart : 1
        how : 1
        hurt : 1
        It : 6
        kept : 1
        love : 1
        my : 2
        pride : 1
        strong : 1
        that : 1
        to : 1
        was : 2
        were : 1
        you : 3
        your : 1 public void printElements(Collection c, PrintStream out) { Iterator iterator = c.iterator(); while (iterator.hasNext()) out.println(iterator.next()); } 本程序将大小写视为不同,所以「How」和「how」被视为不同的字。你可以修改这点,在 StringTokenizer 处理完后将其转成小写即可: String word = st.nextToken().toLowerCase();