`
toflyhigh
  • 浏览: 7622 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

一些面试的小题

阅读更多
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"));
    }
}



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);
    }
}




问题:打印结果是什么? 





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);
        }
    };
}




问题:打印结果是什么?
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics