2

I'm working on a question where I need to compare the values inside a 2D Array in java. For example:

int N = 2, c = 2;
int [][] arr = new int[N][c];

System.out.println("Enter the values to a 2D array: ");

for(int i=0; i<N;i++) {
    for (int j=0;j<c;j++) {
        arr[i][j]=in.nextInt();
    }     
}

So in the above code, the user enters the values inside a 2d array. Now I want to compare if arr[i]>=0 and arr[j]>=0 separately and if yes, I need to perform some other operation on this.

But I'm not able to do it that way. Example:

for(int i=0; i<N;i++) {
    for (int j=0;j<c;j++) {
        if (arr[i]>=0 && arr[j]>=0) {
            //Some operation//
        }
    }
}

Kindly suggest me a way to do this operation - comparing the values individually. Thank you.

1
  • 1
    it is not clear what are you trying to achieve. Commented Jun 13, 2015 at 11:00

3 Answers 3

4

arr1[i] is an array of integers, not an integer, so you can't compare it to an integer. arr1[i][j] is an int, and can be compared to integers.

if (arr[i][j]>=0) is a valid syntax, but it's not clear if that's what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

how do it individually compare there values .. is there any way ?
@Dev Compare what? An array of integers to 0? What's the meaning of such a comparison? Do you wish to compare each number in the array to 0?
1

To compare value of a two dimensional array you should go through with each value of that array.

2x2 array

. .

. .

when i=0, j=0

x .

. .

when i=0, j=1

. x

. .

when i=1, j=0

. .

x .

when i=1, j=1

. .

. x

   for(int i=0; i<N;i++) {
      for (int j=0;j<c;j++) {
         if (arr[i][j]>=your_comparable_value ) {
             //Some operation//
         }
      }
   }

Comments

1

You are storing integers in a 2D array. If it helps, you can visually model 2D arrays by thinking of rows and columns - each row and column pair references it's respective storage location in the array. Example:

arr[0][1] = 5; // sets the value of row [0] column [1] to 5

In the second nested 'for' loop (the one you are having some trouble with), you incorrectly referencing the values of your 2D array. Remember, you must specify the pair - arr[int][int] - in which you wish to reference.

if (arr[i]>=0 && arr[j]>=0); //  incorrect way of referencing the desired respective location in the 2D array

Your revised nested 'for' loop with the syntactically accurate 'if' statement:

for(int i=0; i<N; i++)
    for (int j=0; j<c; j++) // side note: consider using final constant(s) replacing variables N and c. In your case, you are explicitly referencing two integers that store the same value - 2
        if (arr[i][j]>=0)
            System.out.println("Array Position [" + i + "][" + j + "] is greater than or equal to 0");

2 Comments

arr does not expect two integers. arr[i] is a valid array of integers, but can't be compared with 0.
thanks for the help. Understood the logic behind it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.