5

I'm new to java programming and I can't wrap my head around one final question in one of my assignments.

We were told to create a static method that would search a 2-D array and compare the numbers of the 2-D array to an input number...so like this:

private static int[] searchArray(int[][] num, int N){

Now, the part what we're returning is a new one-dimensional array telling the index of the first number in each row that is bigger than the parameter variable N. If no number is bigger than N, then a -1 is returned for that position of the array.

So for example a multi-dimensional array named "A":

4 5 6

8 3 1

7 8 9

2 0 4

If we used this method and did searchArray(A, 5) the answer would be "{2,0,0,-1)"

Cameron
  • 51
  • 1
  • 5

2 Answers2

2

Here is a very good explanation about Java 2D arrays

    int num[][] = {{4,5,6},{8,3,1},{7,8,9}};
    int N = 5;
    int result[] = new int[num.length];
    for(int i=0; i<num.length; i++){
        result[i] = -1;
        for(int j=0; j<num[0].length; j++){
            if( N < num[i][j] ){
                result[i] = j;
                break;
            }
        }
    }

    for(int i=0; i<result.length; i++){
        System.out.println(result[i]);
    }

The first for loop(The one with a for inside it) traverses the 2D array from top to bottom in a left to right direction. This is, first it goes with the 4 then 5,6,8,3,1,7,8,9.

First the result array is created. The length depends of the number of rows of num. result[i] is set to -1 in case there are no numbers bigger than N. if a number bigger than N is found the column index is saved result[i] = j and a break is used to exit the for loop since we just want to find the index of the first number greater than N.

The last for loop just prints the result.

Enrique
  • 9,920
  • 7
  • 47
  • 59
0

Generally when using multi-dimensional arrays you are going to use a nested for loop:

for(int i = 0; i < outerArray.length; i++){
   //this loop searches through each row
   for(int j = 0; j < innerArrays.length; j++) {
     //this loop searches through each column in a given row
     //do your logic code here
   }
}

I won't give you more than the basic structure, as you need to understand the question; you'll be encountering such structures a lot in the future, but this should get you started.

Raskolnikov
  • 286
  • 3
  • 13
  • I understand that much, as most of our assignment was about going through multi-dimensional arrays and replacing values, multiplying them, etc. but the main part I don't understand is how to deal with the whole "row" idea. Do I need to make another nested for statement inside the 2 already given to make it do something after each row or what? – Cameron Dec 13 '10 at 03:18
  • Operations on the row would go inside the outer for loop, which iterates through the rows. Logic placed there is only called once per row. – Raskolnikov Dec 13 '10 at 03:20
  • @Cameron: Raskolnikov has basically given you the majority of the solution to the problem. As he stated in the comment above, the first (outer) loop iterates through the rows, while the second(inner) loop iterates through the items in the row selected from the first loop. You need to add the comparison logic to the code now. – Prabu Dec 13 '10 at 03:29