0

I have this problem but I don't know where it is wrong.

int[] first = new int[2];
first[0] = 3;
first[1] = 7;
int[] second = new int[2];
second[0] = 3;
second[1] = 7;

// print the array elements
System.out.println("first  = [" + first[0] + ", " + first[1] + "]");
System.out.println("second = [" + second[0] + ", " + second[1] + "]");

// see if the elements are the same
if (first[] = second[]) {
    System.out.println("They contain the same elements.");
} else {
    System.out.println("The elements are different.");
}

The expected out put should be like this, for example:

first  = [3, 7]
second = [3, 7]
They contain the same elements.
giang pham
  • 41
  • 4

4 Answers4

0

In java, arrays are Objects, and == (assuming = in the question is a typo) just checks whether both the references point to same object, which is obviously not the case here.

In order to compare the arrays like this, we need to iterate them both and check both have the same elements in the same positions.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

To print the elements of an array, you have to input the element themselves in the System.out.println() method:

System.out.println("First: " + first[0] + ", " + first[1]);

And to evaluate whether two arrays contain the same elements, you have to compare them element by element:

if(first[0] == second[0] && first[1] == second[1]) /*arrays are the same*/;

Usually you would use a loop when comparing arrays.

Maljam
  • 6,244
  • 3
  • 17
  • 30
0

here is the solution

System.out.println("first  = " + Arrays.toString(first);
System.out.println("second = " + Arrays.toString(second);

and in code later

if (Arrays.equals(first,second)) {
    System.out.println("They contain the same elements.");
} else {
    System.out.println("The elements are different.");
}
Jorgovanka
  • 79
  • 6
0

I would extend your example with a third variable so you can understand better

    int[] first = new int[2];
    first[0] = 3;
    first[1] = 7;
    int[] second = new int[2];
    second[0] = 3;
    second[1] = 7;
    int[] third = first;

    if (Arrays.equals(first, second))
        System.out.println("first && second contain the same elements.");
    else
        System.out.println("first && second elements are different.");

    if (Arrays.equals(first, third))
        System.out.println("first && third contain the same elements.");
    else
        System.out.println("first && third elements are different.");

    if (first == second)
        System.out.println("first && second point to the same object.");
    else
        System.out.println("first && second DO NOT point to the same object.");

    if (first == third)
        System.out.println("first && third point to the same object.");
    else
        System.out.println("first && third DO NOT point to the same object.");

Output:

first && second contain the same elements.
first && third contain the same elements.
first && second DO NOT point to the same object.
first && third point to the same object.

The == equal to operator will only check if they are the same object (and third is an alias to first, so they both point to the same object).

While Arrays.equals(a, b) will compare all the elements in both arrays, and return true if they all match.

Jts
  • 3,447
  • 1
  • 11
  • 14