Using Integer[] vs. int[]

10,847

In Java, Integer represents an object, while int is a primitive type. The Integer class supports more functions and can hold null values. In addition, ArrayList can only contain objects such as Integer.

ArrayList<int[]> results = new ArrayList<int[]>();

In the revised code above, int[] would still work because it is considered an object. However, the author may be seeking consistency or would need the extra functionality of the Integer object. It is a matter of the author's preference or ignorance.

Share:
10,847
randomUser47534
Author by

randomUser47534

Updated on June 07, 2022

Comments

  • randomUser47534
    randomUser47534 almost 2 years

    I am trying to solve the following problem: "Write an algorithm to print all the ways of arranging eight queens on an 8x8 chess board so that none of them share the same row, column or diagonal (i.e. no two queens attack each other.)"

    I am having trouble understanding why the author used Integer[] instead of the more common int[], for example in "Integer[] columns" and "ArrayList results" which are parameters to placeQueens. My hypothesis is that this is due to generics in Java, but I'm not entirely sure.

    Code snippet below. Link to complete code at bottom of page.

    public static int GRID_SIZE = 8;
    
    /* Check if (row1, column1) is a valid spot for a queen by checking if there
     * is a queen in the same column or diagonal. We don't need to check it for queens
     * in the same row because the calling placeQueen only attempts to place one queen at
     * a time. We know this row is empty. 
     */
    public static boolean checkValid(Integer[] columns, int row1, int column1) {
        for (int row2 = 0; row2 < row1; row2++) {
            int column2 = columns[row2];
            /* Check if (row2, column2) invalidates (row1, column1) as a queen spot. */
    
            /* Check if rows have a queen in the same column */
            if (column1 == column2) { 
                return false;
            }
    
            /* Check diagonals: if the distance between the columns equals the distance
             * between the rows, then they're in the same diagonal.
             */
            int columnDistance = Math.abs(column2 - column1); 
            int rowDistance = row1 - row2; // row1 > row2, so no need to use absolute value
            if (columnDistance == rowDistance) {
                return false;
            }
        }
        return true;
    }
    
    public static void placeQueens(int row, Integer[] columns, ArrayList<Integer[]> results) {
        if (row == GRID_SIZE) { // Found valid placement
            results.add(columns.clone()); 
        } else {
            for (int col = 0; col < GRID_SIZE; col++) {         
                if (checkValid(columns, row, col)) {
                    columns[row] = col; // Place queen
                    placeQueens(row + 1, columns, results); 
                }       
            }
        }
    }
    

    Source for question/code: Cracking the Coding Interview. Link to complete code: https://github.com/gaylemcd/ctci/blob/master/java/Chapter%209/Question9_9/Question.java