文档

Java™ 教程
导航: 集合
课程: 实现
首页 > 集合 > 实现

问题和练习的答案:

问题

  1. 问题:你计划编写一个程序,使用了几个基本的集合接口:SetListQueueMap。你不确定哪些实现会最好地工作,所以决定在实际情况下先使用通用实现。这些通用实现是哪些?
    答案:
    SetHashSet
    ListArrayList
    QueueLinkedList
    MapHashMap
  2. 问题:如果你需要一个提供值排序迭代的Set实现,应该使用哪个类?
    答案:
    TreeSet保证排序集是按照元素的自然顺序或提供的Comparator进行排序的。
  3. 问题:你使用哪个类来访问包装器实现?
    答案:
    你使用Collections类,该类提供了操作或返回集合的静态方法。

练习

  1. 练习:编写一个程序,从第一个命令行参数指定的文本文件中读取内容到一个List中。然后程序应该随机打印文件中的行数,打印的行数由第二个命令行参数指定。编写程序时,应一次性分配正确大小的集合,而不是逐步扩展读取文件时的集合。提示:要确定文件中的行数,使用java.io.File.length获取文件的大小,然后除以平均行大小的一个假设值。
    答案:
    由于我们是随机访问List,我们将使用ArrayList。我们通过将文件大小除以50来估计行数。然后将这个数字加倍,因为过度估计比低估更有效率。
    import java.util.*;
    import java.io.*;
    
    public class FileList {
        public static void main(String[] args) {
            final int assumedLineLength = 50;
            File file = new File(args[0]);
            List<String> fileList = 
                new ArrayList<String>((int)(file.length() / assumedLineLength) * 2);
            BufferedReader reader = null;
            int lineCount = 0;
            try {
                reader = new BufferedReader(new FileReader(file));
                for (String line = reader.readLine(); line != null;
                        line = reader.readLine()) {
                    fileList.add(line);
                    lineCount++;
                }
            } catch (IOException e) {
                System.err.format("Could not read %s: %s%n", file, e);
                System.exit(1);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {}
                }
            }
            int repeats = Integer.parseInt(args[1]);
            Random random = new Random();
            for (int i = 0; i < repeats; i++) {
                System.out.format("%d: %s%n", i,
                        fileList.get(random.nextInt(lineCount - 1)));
            }
        }
    }
    
    这个程序实际上大部分时间都花在读取文件上,所以预先分配ArrayList对其性能几乎没有影响。提前指定初始容量更有用的情况是当你的程序反复创建大型ArrayList对象而没有介入I/O的时候。

上一页:问题和练习:实现