Content of the array is not being printed, but reference to the object

Asked

Viewed 45 times

0

Hello, I’m making an algorithm that prints only the repeating elements, e.g.: {2,2,3,4,5,5} -> {2,5} I don’t know if my algorithm is correct, but I can see the array itself, it appears something like [I@71e7a66b. Someone can help me?

public class main {
public static void main(String[] args) {
    int[] array = {1,1,1,3,2,2,7,5,6,14,12,23,3,3,2};
    int[] newArray = new int[array.length];
    int count = 0;
    int position = 0;

    for(int i = 0; i < array.length - 1; i++) {
        for(int j = i + 1; j < array.length; j++) {
            if(array[i] == array[j]) {
                count++;
                }
            }
            if(count > 0){
                newArray[position] = array[i];
                position++;
            }
            count = 0;
        }

    System.out.println(newArray);

}
    }

2 answers

0


You can use method Arrays#toString:

import java.util.Arrays;

...

System.out.println(Arrays.toString(newArray));

Arrays#toString

Returns a string representation of the Contents of the specified array.

In free translation:

Returns the string representation of the content specified in the array.

0

You are printing on the screen only the reference of newArray with

System.out.println(newArray)

because it is printing it and not its items, for such you must use a toString method, as in the answer above, or even a FOR for each newArray item

 for(int = 0; i < newArray.lentgh; i++){
    System.out.println(newArray[i]);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.