9

I would like to know what is the best, fastest and easiest way to compare between 2-dimension arrays of integer. the length of arrays is the same. (one of the array's is temporary array)

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
firestruq
  • 719
  • 6
  • 15
  • 27
  • Sounds like a homework question? Go with the easiest method first. Start with one array iterate through the other for each element of the first applying your conditional check on each iteration step. That method is very inefficient and you can likely speed things depending on the structure of the arrays (i.e. are they sorted), what you're search for, etc. Ask yourself why its bad and what conditions would make things easier to find what you want. – Rob Segal Mar 31 '10 at 19:10
  • you need to add a bit more definition to your question so we can help. Do you have 2 1-D arrays or 2 2-D arrays/tables. What are you using to store your arrays? Integer[] or an implementation of List<>? What have you tried and why don't you like it? – Simon Mar 31 '10 at 19:11
  • When you say "compare", do you mean test for elementwise equality? If so, `Arrays.equals` is your friend. If by "compare" you mean something else, can you elaborate? – Will Mar 31 '10 at 19:17
  • The purpose is just to check that the two arrays is the same that i could terminate the app. it's two arrays of two-dimension with the same length both. – firestruq Mar 31 '10 at 19:17
  • just need to see if the value is the same – firestruq Mar 31 '10 at 19:17

2 Answers2

13

Edan wrote:

just need to see if the value is the same

If you want to check that a[i][j] equals b[i][j] for all elements, just use Arrays.deepEquals(a, b).

Will
  • 5,429
  • 4
  • 22
  • 31
4

Look at java.util.Arrays; it has many array utilities that you should familiarize yourself with.

import java.util.Arrays;

int[][] arr1;
int[][] arr2;
//...
if (Arrays.deepEquals(arr1, arr2)) //...

From the API:

Returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.

Note that in Java, int[][] is a subtype of Object[]. Java doesn't really have true two dimensional arrays. It has array of arrays.

The difference between equals and deepEquals for nested arrays is shown here (note that by default Java initializes int arrays with zeroes as elements).

    import java.util.Arrays;
    //...

    System.out.println((new int[1]).equals(new int[1]));
    // prints "false"

    System.out.println(Arrays.equals(
        new int[1],
        new int[1]
    )); // prints "true"
    // invoked equals(int[], int[]) overload

    System.out.println(Arrays.equals(
        new int[1][1],
        new int[1][1]
    )); // prints "false"
    // invoked equals(Object[], Object[]) overload

    System.out.println(Arrays.deepEquals(
        new int[1][1],
        new int[1][1]
    )); // prints "true"

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623