The contents of the array are not being printed, but the reference to the object [duplicate]

0

Hello, I'm making an algorithm that prints only repeating elements, eg: {2,2,3,4,5,5} -> {2,5} I do not know if my algorithm is correct, but I can not see the array itself, something like [I @ 71e7a66b. Can anyone 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);

}
    }
    
asked by anonymous 19.02.2018 / 14:05

2 answers

0

You can use Arrays#toString method:

import java.util.Arrays;

...

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

Arrays # toString

     

Returns the string representation of the contents of the specified array.

Free translation:

  

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

    
19.02.2018 / 14:06
0

You are printing on the screen only the newArray reference with

System.out.println(newArray)

Because you are printing it and not your items, you must either use a toString method, as in the above answer, or a FOR for each newArray item

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