0

次のコードは、合計が x になる整数のペアを返します。たとえば、arr {1, 2, 3, 4, 5] で x が 7 の場合、リストには {3, 4} と {2, 5} が含まれている必要があります。主な目標は、プライベート メソッドでパラメーターの検証を実行する方法を理解することです。質問はコメント内にネストされています。提案は、尋ねられた質問のみに限定してください。私の質問をチェックするためにコードに飛び込んでくれてありがとう。

public static List<Pair> getPairsFromPositiveArray(int[] arr, int x) {
    // check for all positive integers
    for (int i : arr) {   // if arr is null, then this loop would throw NPE. So no need to make an exclicit check for null.
        if (i < 0) throw new IllegalArgumentException("No integer should be negative.");
    }
    final List<Pair> list = new ArrayList<Pair>();
    getPair(arr, x, list);
    return list;
}

private static void getPair(int[] arr, int x, List<Pair> list) {
    // QUESTION 1: Should check of "all positive integers" be done here too ?

    /*
     * QUESTION 2: 
     * list is data structure which we created internally ( user did not provide it )  
     * Does anyone suggest, it throw an NPE or do an explicit assert check for list != null ? 
     */
    assert list != null;  // form my understanding of e effective java. 
    assert arr != null;   // form my understanding of e effective java. 

    final Set<Integer> set = new HashSet<Integer>();
    /*
     * QUESTION 3:
     * arr is a data structure which was input by the user.
     * Should we check for assert arr != null or let loop throw a NPE ?
     */
    for (int i : arr) { 
        if (set.contains(i)) {
            System.out.println(i + " : ");
            list.add(new Pair(i, x - i));
        } else {
            set.add(x - i);
        }
    }
}
4

3 に答える 3

2

質問 1: ここでも「すべての正の整数」のチェックを行う必要がありますか?

回答:いいえ。リストを取得するgetPairsFromPositiveArray()前に電話することになるのでgetPair()

質問 2: * list は内部で作成したデータ構造です (ユーザーは提供しませんでした)
* NPE をスローするか、list != null に対して明示的な assert チェックを行うことを提案する人はいますか?

答え: 主張する

質問 3: * arr は、ユーザーが入力したデータ構造です。* assert arr != null をチェックするか、ループで NPE をスローするかをチェックする必要があります

答え:

  • 常に健全性チェックを実行します....はい、 arr != null をチェックしてください

  • NPE のスローよりもアサートを常に好む

于 2013-12-20T06:59:32.637 に答える
0

質問 1:for()ループ内で実行します。そうしないと、2 回ループする必要があります。しかし...そのような場合、呼び出しプログラムは何を期待していますか?

質問 2: リストがnull初期化されないのはなぜですか? arrである場合null、はい、問題があります。繰り返しになりますが、この場合に何が起こると思いますか?

質問 3: 質問arr == nullごとに確認します。

通常、優れたプログラミングは防御的プログラミングです。提供された入力が間違っているか間違っていると常に想定してください。一方、合理的で単純な方法では、物事を過度に複雑にする必要はありません。

于 2013-12-20T06:49:42.827 に答える
0

答えは実に明快で、

public static List<Pair> getPairsFromPositiveArray(int[] arr, int x) 
{       
     // if arr is null, then this loop would throw NPE. So no need to make an explicit check for null.

     //     for (int i : arr) //No need we can check downward 
     //     {  
     //     }
    final List<Pair> list = new ArrayList<Pair>();
    getPair(arr, x, list);
    return list;
}

private static void getPair(int[] arr, int x, List<Pair> list) throws NullPointerException //TELLS THIS WILL THROW NPE
{
    // QUESTION 1: Should check of "all positive integers" be done here too ?

    /*
     * QUESTION 2: 
     * list is data structure which we created internally ( user did not provide it )  
     * Does anyone suggest, it throw an NPE or do an explicit assert check for list != null ? 
     */
    //NO CHECK ARE REQUIRED HERE AS List<Pair> list IS NULL THEN IF WE REASSIGN IT ALSO THEN IT WILL NOT BE REFLECTED BACK TO ORIGINAL, YOU CAN THROW A CUSTOM EXCPETION OR NPE AND DOCUMENT IT (ADD TO METHOD THROWS NPE)   


    final Set<Integer> set = new HashSet<Integer>();
    /*
     * QUESTION 3:
     * arr is a data structure which was input by the user.
     * Should we check for assert arr != null or let loop throw a NPE ?   
     //NO CHECK ARE REQUIRED HERE AS int[] arr IS NULL THEN IF WE REASSIGN IT ALSO THEN IT WILL NOT BE REFLECTED BACK TO ORIGINAL, YOU CAN THROW A CUSTOM EXCPETION OR NPE AND DOCUMENT IT (ADD TO METHOD THROWS NPE)
     */
    for (int i : arr) 
    { 
        if (i < 0) // ANSWER TO QUESTION 1
            throw new IllegalArgumentException("No integer should be negative.");

        if (set.contains(i)) 
        {
            System.out.println(i + " : ");
            list.add(new Pair(i, x - i));
        }
        else 
        {
            set.add(x - i);
        }
    }
}
于 2013-12-20T07:15:07.853 に答える