Domanda

My homework assignment is asking to output the sum of a specified column of a Jagged 2D Array. I've seen other solutions that show how to get the sum of ALL columns, but not a specific one. The issue I'm running into is that I get a java.lang.ArrayIndexOutOfBoundsException if a column is entered and no element exists in a row of the 2D array.

// returns sum of specified column 'col' of 2D jagged array
public static int columnSum(int[][] array, int col) {
    int sum = 0;

// for loop traverses through array and adds together only items in a specified column
    for (int j = 0; j < array[col].length; j++) {
    sum += array[j][col];
    }

return sum;
} // end columnSum()

Example: Ragged Array Input (class is named RaggedArray)

int[][] ragArray = { {1,2,3}, 
                     {4,5}, 
                     {6,7,8,9} };

System.out.println(RaggedArray.columnSum(ragArray, 2));

This obviously gives me an ArrayIndexOutOfBoundsException, but I don't know how to fix it if a specified column is asked for as an argument. Any ideas? I appreciate any help or suggestions!

È stato utile?

Soluzione 2

Here is another solution I found.

// returns sum of the column 'col' of array
    public static int columnSum(int[][] array, int col) {
    int sum = 0;

    // for loop traverses through array and adds together only items in a specified column
    try {
        for (int j = 0; j < array.length; j++) {
            if (col < array[j].length)
                sum += array[j][col];   
           }
        }
    catch (ArrayIndexOutOfBoundsException e){
        }

    return sum;
} // end columnSum()

Altri suggerimenti

In your loop, do a

try{
  sum += array[j][col];
}catch(ArrayIndexOutOfBoundsException e){

}

block, where it simply skips over if there is nothing, and keeps going to the next one. you will have to import that exception as well. If you run into trouble, just look up how try/catch blocks work

public class JaggedArrayColSum {
    int[] jackedArrayColSum(int arr[][]){
        
        int sum;
        int maxLen=0;
        boolean status;
        
        
        //START #MAX LENGTH
        //Finding the max length of inner array
        for(var a=0;a<arr.length;a++) {
            status=true;
            for(var b=0;b<arr.length;b++) {
                if(a==b)continue;
                if(arr[a].length<arr[b].length) {
                    status=false;
                    break;
                }
                if(status)maxLen=arr[a].length;
            }
        }
        //END #MAX LENGTH
        
        //START #SUM JAGGED ARRAY 
        int [ ] arr1=new int[maxLen];
        for(var a=0;a<maxLen;a++) {
            sum=0;
            for(var b=0;b<arr.length;b++) {
                if(arr[b].length>a)sum+=arr[b][a];
            }
            arr1[a]=sum; //Adding values to the the return array index
        }
        //END #SUM JAGGED ARRAY 
        
        
        return arr1;
    }
    public static void main ( String [ ] args ) {
        int [][]arr= {{1},{1,3},{1,2,3,4},{1,2,3,4,5,6,7}};
        for(int x:new JaggedArrayColSum().jackedArrayColSum ( arr ))System.out.print ( x +" " ) ;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top