Help with Arrays in Java Required

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Lucifer666, Dec 7, 2012.

Dec 7, 2012
  1. Lucifer666
    OP

    Member Lucifer666 all the world needs is me

    Joined:
    Apr 22, 2011
    Messages:
    1,393
    Location:
    The Fourth Dimension
    Country:
    Antarctica
    Java isn't exactly my favourite programming language, but for this particular task I'm going to need it, though I'm quite the layman.

    So suppose I've created an array:
    Code:
    int Array[][] = new int[3][3]
    and throughout my class, members of the array are assigned numbers.
    Eventually I reach a point where I'd like to reference them in an "if...then" statement.

    The condition for my if statement is "if everything in Array[1][] is equal to x"; however, I've never dealt with calling an entire row of a multidimensional array in Java, so I'm not quite sure how it's done.

    With something as simple as a [3][3] array, I've just been using:
    Code:
    if (Array[1][0], Array[1][1], Array[1][2] = x) { 
    which is obviously not the most efficient way to do this, and I'd imagine it can be quite problematic on a larger scale.

    So, in Java, how would I call all members of an entire row in a multidimensional array for an if statement? Cheers.
     
  2. Daku93

    Member Daku93 GBAtemp Regular

    Joined:
    Jul 24, 2008
    Messages:
    286
    Location:
    Düsseldorf, Germany
    Country:
    Germany
    You could do something like
    Code:
    boolean isEqual = true;
    int x = //Value you want to compare to.
    for (int i = 0; i < array[1].length; i++) {
         if (array[1][i] != x) {
              isEqual = false;
              break;
         }
    }
    
    so isEqual will tell you if all of them are equal to x or not.
     

Share This Page