cs 108 03(デバッグ、java汎用性)

13116 ワード

Debuger
Great questions
These questions will solve most bugs:
what method shows the symptom ? what lines of code produces that symptom ?
what is the state of the receiver object in that code ? what were the param values passed in ?
if it’s an exception, what does the exception error message say – null pointer? array access? Somethimes the exception name can be very informative.
チューニング方法
Eclipse debugger、使いやすい
println()
コードの一部を注釈する
Truths of Debugging
  • 直感は重要で、直感の考えをテストすることができますが、直感が実際に衝突したとき、実際に勝利します.
  • 簡単なコードも重大なバグを引き起こし、簡単なコードを無視しないでください.よく問題が発生します.
  • 自分で定義した変数に注意して、プログラムの中で現れたバグは、往々にしてあなたが定義した変数があなたの望む値ではありません.
  • もしあなたのプログラムが1分前に正常に実行できたら、今はだめです.前回何を変更しましたか.注意:50行のcodeを書くたびにテストすると、プログラムに問題が発生したとき、どの50行に問題が発生したかがわかります.
  • コードを勝手に変更してバグを追跡しないでください.これにより、新しいバグをもたらす可能性があります.
  • もしあなたがいくつかの間違いがあなたがずっと追跡していた間違いと関係がないことを発見したら、まずこれらの間違いを解決しましょう.これらの間違いはあなたがずっと追跡していたバグと関係があるかもしれませんが、あなたはまだ考えていません.
  • Java汎用性(Generics)
  • Using a generic class, like using ArrayList
  • Writing generic code with a simple or type parameter
  • Writing generic code with a type parameter

  • Use Generic Class
    ArrayList<String> strings = new ArrayList<String>();
    strings.add("hi");
    strings.add("there");
    String s = strings.get(0);

    <!--
    .csharpcode, .csharpcode pre
    {
    font-size: small;
    color: black;
    font-family: consolas, "Courier New", courier, monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
    }
    .csharpcode pre { margin: 0em; }
    .csharpcode .rem { color: #008000; }
    .csharpcode .kwrd { color: #0000ff; }
    .csharpcode .str { color: #006080; }
    .csharpcode .op { color: #0000c0; }
    .csharpcode .preproc { color: #cc6633; }
    .csharpcode .asp { background-color: #ffff00; }
    .csharpcode .html { color: #800000; }
    .csharpcode .attr { color: #ff0000; }
    .csharpcode .alt
    {
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
    }
    .csharpcode .lnum { color: #606060; }
    -->
    リサイクル
    List<String> strings = ...
    for (String s: strings) {
      System.out.println(s);
    }

    例:
    public static void dempList() {
      List<String> a = new ArrayList<String>();
      a.add("Don't");
      a.add("blame");
      a.add("me");
    
      for (String str: a) {
        System.out.println(str);
      }
    
      Iterator<String> it = a.iterator();
      while (it.hasNext()) {
        String string = it.next();
        System.out.println(String);
      }
      List<Integer> ints = new ArrayList<Integer>();
      for (int i = 0; i<10; i++) {
        ints.add(new Integer(i * i));
      }
      int sum = ints.get(0).intValue() + ints.get(1).intValue();
    
      sum = ints.get(0) + ints.get(1);
    
      // Generic Map Example Code
      public static void demoMap() {
        HashMap<Integer, String>map = new HashMap<Integer, String>();
        map.put(new Integer(1), "one");
        map.put(new Integer(2), "two");
        map.put(3, "three");  //     
        map.put(4, "four");
    
       String s = nap.get(new Integer(3));
       s = map.get(3)  //     
       
      HashMap<String, List<Integer>> counts = new HashMap<String, List<Integer>>();
      List<Integer> evens = new ArrayList<Integer>();
      evens.add(2);
      evens.add(4);
      evens.add(6);
      counts.put("even", evens);
    
      List<Integer> evens2 = counts.get("evens");
     

    Define a Generic Class/Method
    you can define your own class as a generic class. the class definithion code is parameterized by a type, typically called. This is more or less what ArrayList does. At the very start of the class, the parameter is added like this: public calss Foo
    このTには以下の制限がある:(Tの本質は普通のタイプのようなもの)
    - declare variables, parameters, and return types of the type T
    - use = on T pointers
    - call methods that work on all Objects, like .equals()
    覚えておいてください:where you see"T"、it is just replaced by"Object"to produce the code for runtime.So the ArrayListcode and the ArrayListcode...those two are actually just the ArrayListcode at runtime.
    例:
    public class Pair<T> {
      private T a;
      private T b;
      private List<T> unused;
    
      public Pair(T a, T b) {
        this. a = a;
        this.b = b;
      }
      public T getA() {
        return a;
      }
      public T getB() {
        return b;
      }
      public void swap() {
        T temp = a;
        a = b;
        b = temp;
     }
     
     public boolean isSame() {
        return a.equals(b);
    }
    
    public boolean contains(T elem) {
      return (a.equals(elem) || b.equals(elem));
    }
    public static void main(String[] args) {
      
    // integer      
    Pair<Integer> ipair = new Pair<Integer>(1, 2);
      Integer a = ipair.getA();
      int b = ipair.getB();  //    
      
    // String       
      Pair<String> spair = new Pair<String>("hi", "there");
      String s = spair.getA();
    }

    <!--
    .csharpcode, .csharpcode pre
    {
    font-size: small;
    color: black;
    font-family: consolas, "Courier New", courier, monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
    }
    .csharpcode pre { margin: 0em; }
    .csharpcode .rem { color: #008000; }
    .csharpcode .kwrd { color: #0000ff; }
    .csharpcode .str { color: #006080; }
    .csharpcode .op { color: #0000c0; }
    .csharpcode .preproc { color: #cc6633; }
    .csharpcode .asp { background-color: #ffff00; }
    .csharpcode .html { color: #800000; }
    .csharpcode .attr { color: #ff0000; }
    .csharpcode .alt
    {
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
    }
    .csharpcode .lnum { color: #606060; }
    -->
    Generic Method
    クラス全体で汎用を使用する場合、ある方法に対して汎用を使用できます.構文:publicvoid foo(Listlist)
    例:
    public static <T> void removeAdjacent(Collection<T> coll) {
      Iterator<T> it = coll.iterator();
      T last = null;
      while (it.hasNext()) {
        T curr = it.next();
        if (curr == las) it.remove();
        last = curr;
      }
    }

    <!--
    .csharpcode, .csharpcode pre
    {
    font-size: small;
    color: black;
    font-family: consolas, "Courier New", courier, monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
    }
    .csharpcode pre { margin: 0em; }
    .csharpcode .rem { color: #008000; }
    .csharpcode .kwrd { color: #0000ff; }
    .csharpcode .str { color: #006080; }
    .csharpcode .op { color: #0000c0; }
    .csharpcode .preproc { color: #cc6633; }
    .csharpcode .asp { background-color: #ffff00; }
    .csharpcode .html { color: #800000; }
    .csharpcode .attr { color: #ff0000; }
    .csharpcode .alt
    {
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
    }
    .csharpcode .lnum { color: #606060; }
    -->
    Method – use a type on the method to identify what type of element is in the collection. The goes just before the return type. T can be used to decalre variables, return types, etc. This is ok, but slightly heavyweight, since in this case we actually don’t care what type of thing is in there. This removes elements that are == to an adjacent element.
    ?/T with “extends” Generics
    extendsは、with a–for any T value,we can assume it is a Number subclass,so.intValue()などのTを制限することができる.
    例:
    public class PairNumber <T extends Number> {
      private T a;
      private T b;
    
      public PairNumber( T a, T b) {
        this.a = a;
        this.b = b;
      }
      public int sum() {
        return (a.intValue() + b.intValue());
      }

    ?/T Extends Method
    <!--
    .csharpcode, .csharpcode pre
    {
    font-size: small;
    color: black;
    font-family: consolas, "Courier New", courier, monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
    }
    .csharpcode pre { margin: 0em; }
    .csharpcode .rem { color: #008000; }
    .csharpcode .kwrd { color: #0000ff; }
    .csharpcode .str { color: #006080; }
    .csharpcode .op { color: #0000c0; }
    .csharpcode .preproc { color: #cc6633; }
    .csharpcode .asp { background-color: #ffff00; }
    .csharpcode .html { color: #800000; }
    .csharpcode .attr { color: #ff0000; }
    .csharpcode .alt
    {
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
    }
    .csharpcode .lnum { color: #606060; }
    -->
    public static int sumAll(Collection<? extends Number> nums) {
      int sum = 0;
      for (Number num : nums) {
        sum += num.intValue(0;
      }
      return sum;
    }

    <!--
    .csharpcode, .csharpcode pre
    {
    font-size: small;
    color: black;
    font-family: consolas, "Courier New", courier, monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
    }
    .csharpcode pre { margin: 0em; }
    .csharpcode .rem { color: #008000; }
    .csharpcode .kwrd { color: #0000ff; }
    .csharpcode .str { color: #006080; }
    .csharpcode .op { color: #0000c0; }
    .csharpcode .preproc { color: #cc6633; }
    .csharpcode .asp { background-color: #ffff00; }
    .csharpcode .html { color: #800000; }
    .csharpcode .attr { color: #ff0000; }
    .csharpcode .alt
    {
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
    }
    .csharpcode .lnum { color: #606060; }
    -->