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 吗?

答:断言

QUESTION 3: * arr 是用户输入的数据结构。* 我们应该检查 assert arr != null 还是让循环抛出 NPE

答:

  • 始终执行完整性检查....所以是的,请检查 arr != null

  • 总是更喜欢断言而不是抛出 NPE

于 2013-12-20T06:59:32.637 回答
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 回答
0

问题1:我会在for()循环中执行,否则您必须循环两次。但是......在这种情况下,您的调用程序期望什么?

问题2:如果list是null为什么不初始化呢?如果arrnull,那么是的,有问题。你又期望在这种情况下会发生什么?

问题 3:您检查arr == null每个问题。

通常好的编程是防御性编程。始终假设提供了错误或错误的输入。另一方面,对于一个合理的简单方法,您不必将事情过度复杂化。

于 2013-12-20T06:49:42.827 回答