海闊天空

          I'm on my way!
          隨筆 - 17, 文章 - 69, 評論 - 21, 引用 - 0
          數(shù)據(jù)加載中……

          很有深度的幾個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()這個方法的話,應(yīng)該不會出錯。方法的功能參考文檔。

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



          問題:打印結(jié)果是什么?

          這個看第一遍可能會出錯,不過也算容易理解。

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



          問題:輸出結(jié)果是什么?

          理解如何正確的設(shè)計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);
          }
          };
          }



          問題:打印結(jié)果是什么?

          如果看過《Java Puzzlers》這本書的話應(yīng)該容易發(fā)現(xiàn)問題。

          5.Cogito Ergo Sum
          Java code


          import java.util.LinkedHashMap;
          import java.util.Map;

          public enum RomanNumeral {

          I(
          1), V(5), X(10), L(50), C(100), D(500), M(1000);
          private static Map<Integer, RomanNumeral> map = new LinkedHashMap<Integer, RomanNumeral>();
          public final int val;

          RomanNumeral(
          int val) {
          this.val = val;
          storeInMap();
          }

          private void storeInMap() {
          map.put(val,
          this);
          }

          public static RomanNumeral fromInt(int val) {
          return map.get(val);
          }

          public static void main(String[] args) {
          int sum = 0;
          for (int i = 0; i < 1000; i++) {
          if (fromInt(i) != null) {
          sum
          += i;
          }
          }
          System.out.println(sum);
          }
          }



          問題:打印結(jié)果是什么?

          如果理解java加載類和創(chuàng)建對象的順序的話這個問題容易理解。

          6.Thread Friendly
          Java code


          public class ThreadFriendly {
          ThreadLocal
          <Value> threadLocalPart = new ThreadLocal<Value>();
          class Value{
          final int i;
          Value(
          int i){
          this.i = i;
          }
          }

          ThreadFriendly setThreadVal(
          int i){
          threadLocalPart.set(
          new Value(i));
          return this;
          }

          int getThreadVal(){
          return threadLocalPart.get().i;
          }

          public static void main(String[] args) {
          int sum = 0;
          for(int i = -500000;i<=500000;i++){
          sum
          += new ThreadFriendly().setThreadVal(i).getThreadVal();
          }
          System.out.println(sum);
          }
          }



          問題:打印結(jié)果是什么?

          理解內(nèi)部類和ThreadLocal。

          7.When Words Collide
          Java code

          public class PrintWords {
          public static void main(String[] args) {
          System.out.println(
          Words.FIRST
          + " " + Words.SECOND + " " + Words.THIRD
          );
          }
          }

          public class Words{
          public static final String FIRST = "the";
          public static final String SECOND = null;
          public static final String THIRD = "set";
          }


          編譯PrintWords.java文件。
          修改Words.java文件為
          Java code

          public class Words{
          public static final String FIRST = "physics";
          public static final String SECOND = "chemistry";
          public static final String THIRD = "biology";
          }


          問題:再次編譯運行PrintWords.java,打印結(jié)果是什么?

          需要了解常量折疊現(xiàn)象,理解什么是常量。

          posted on 2009-08-05 10:32 石頭@ 閱讀(407) 評論(0)  編輯  收藏 所屬分類: java_base

          主站蜘蛛池模板: 松溪县| 栾城县| 长沙县| 武冈市| 勃利县| 宾阳县| 庄河市| 板桥市| 潮州市| 布拖县| 崇明县| 桦南县| 思南县| 仁怀市| 黄龙县| 雷波县| 泗水县| 衢州市| 兴城市| 陇西县| 九龙县| 濉溪县| 包头市| 乡宁县| 南华县| 虞城县| 通州区| 宁津县| 宜君县| 昌邑市| 册亨县| 清水河县| 蒙自县| 巴东县| 北川| 肥东县| 双流县| 江川县| 镇雄县| 九龙县| 洪雅县|