日期:2014-05-20  浏览次数:20841 次

几个谜题,深入的了解java
在2009年的JavaOne大会上,Joshua Bloch和Neal Gafter又为我们带来的7道谜题,挺有意思的。大家不妨看看。
摘自:
Return of the Puzzlers: Schlock and Awe
Joshua Bloch, Google, Inc.; Neal Gafter, Microsoft
http://developers.sun.com/learning/javaoneonline/sessions/2009/pdf/TS-5186.pdf
1.Life's Persistent Questions
Java code

public class SimpleQuestion {

    static boolean yesOrNo(String s) {
        s = s.toLowerCase();
        if (s.equals("yes") || s.equals("y") || s.equals("t")) {
            s = "true";
        }
        return Boolean.getBoolean(s);
    }

    public static void main(String[] args) {
        System.out.println(yesOrNo("true") + " " + yesOrNo("Yes"));
    }
}


问题:程序打印什么?
如果熟悉Boolean.getBoolean()这个方法的话,应该不会出错。方法的功能参考文档。

2.Instruments of Tortue
Java code


import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

public class InstrumentedHashSet<E> extends HashSet<E> {
    private int addCount = 0;
    @Override
    public boolean add(E e){
        addCount++;
        return super.add(e);
    }

    @Override
    public boolean addAll(Collection<? extends E> c){
        addCount += c.size();
        return super.addAll(c);
    }

    public static void main(String[] args) {
        InstrumentedHashSet<String> s = new InstrumentedHashSet<String>();
        s.addAll(Arrays.asList("Accordion","Banjo","Kazoo"));
        System.out.println(s.addCount);
    }
}



问题:打印结果是什么?

这个看第一遍可能会出错,不过也算容易理解。

3.Iterator Titillator
Java code

import java.util.Iterator;
import java.util.NoSuchElementException;

public abstract class AbstractIterator<T> implements Iterator<T> {

    T next = nextElement();

    public boolean hasNext() {
        return next != null;
    }

    public T next() {
        if (next == null) {
            throw new NoSuchElementException();
        }
        T result = next;
        next = nextElement();
        return result;
    }

    public void remove() {
        throw new UnsupportedOperationException();
    }

    protected abstract T nextElement();

    private static Iterator<Character> test(final String s) {
        return new AbstractIterator<Character>() {

            private int cursor = 0;

            protected Character nextElement() {
                return cursor == s.length() ? null : s.charAt(cursor++);
            }
        };
    }

    public static void main(String[] args) {
        for (Iterator<Character> i = test("OPS"); i.hasNext();) {
            System.out.print(i.next());
        }
    }
}



问题:输出结果是什么?

理解如何正确的设计Iterator。

4.Search for the One
Java code


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class Searching {
    public static void main(String[] args) {
        String[] strings = { "0", "1", "2", "3", "4", "5"};

        List<Integer> integers = new ArrayList<Integer>();
        for(String s : strings){
            integers.add(Integer.valueOf(s));
        }

        System.out.println(Collections.binarySearch(integers, 1,cmp));
    }

    static Comparator<Integer> cmp = new Comparator<Integer>(){
        public int compare(Integer i,Integer j){
            return i<j?-1:(i==j?0:1);
        }
    };
}