Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Checking arrays equal  (Read 180 times)
0 Members and 1 Guest are viewing this topic.
Offline JESTERRRRRR

Jr. Member
**

Posts: 63
Medals: 1



« on: 2012-01-29 14:23:54 »

True noob question here. Need to check two arrays are equal (in contained values), after failing googled and got Arrays.equals(a,b,)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void checkColours(int[][] p)
    {
        if(!Arrays.equals(colourOffset, p))
        {
        System.out.println("\nArrays not equal");
        System.out.println("Recieved array: ");
        for(int i = 0; i < 7; i++)
        {
             for(int n = 0; n < 3; n++)
             {
                System.out.print(p[i][n]);
            }
        }
        System.out.println("\nStored array: ");
        for(int i = 0; i < 7; i++)
        {
             for(int n = 0; n < 3; n++)
             {
                System.out.print(colourOffset[i][n]);
            }
        }


Output:

Arrays not equal
Recieved array:
000000000000000000000
Stored array:
000000000000000000000


Arrays are both created: colourOffset = new int[7][3];  int col[][] = new int[7][3]; (that one is sent as p)

What am I doing wrong?


Offline Waterwolf

JGO n00b
*

Posts: 36
Medals: 2



« Reply #1 on: 2012-01-29 14:38:15 »

Probably because its a multidimensional array and Arrays.equals is therefore comparing int[] array references. You could for loop through first dimension and use Arrays.equals on each second dimension array.
Offline JESTERRRRRR

Jr. Member
**

Posts: 63
Medals: 1



« Reply #2 on: 2012-01-29 14:48:20 »

ah of course you're right thanks

Edit: Arrays.deepEquals(a,b) solves the problem
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.079 seconds with 20 queries.