-2

Here is the problem I am working on: "Write a method that returns a new array by eliminating the duplicate values in the array using the following method header:

public static int[] eliminateDuplicates(int[] list)

Write a test program that read in ten integers, invokes the method and displays the result."

Here is the code I have:

 public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      int[] list = new int[10];

      System.out.print("Enter ten numbers:");

      for (int i = 0; i < list.length; i++) {
       list[i] = input.nextInt();
      }

      System.out.print("The distinct numbers are: ");
      for(int i = 0; i < 10; i++){
          if( list[i] != -1){
      System.out.print(eliminateDuplicates(list)); 
          }
      } 
 }
     public static int[] eliminateDuplicates(int[] list) {
         for(int i = 0; i < 10; i++){
             for(int j = i + 1; j < 10; j++){
                if (list[i] == list[j]){
                    list[i]= -1;
                     }
             }  
         }
         return list;        

} }

My issue is my output is really strange, this is what I'm getting "[I@55f96302[I@55f96302[I@55f96302[I@55f96302[I@55f96302[I@55f96302[I@55f96302"

Can anyone tell me where I'm going wrong? Or a suggestion of where to start fixing it? I'm still a beginner, so I haven't learned a whole lot (like hashsets or anything).

You have my many thanks

  • You have to loop over and print out each element of an array. `System.out.println(array)` doesn't do what you think it does. – Kevin Feb 12 '17 at 21:22
  • @Kevin - No need to loop over all elements. There are the handy `java.util.Arrays.toString(...)` methods. :) – Ted Hopp Feb 12 '17 at 21:30
  • @Kevin - You are right, of course, that it's still a loop. I should have said "No need to loop over all elements _yourself_." – Ted Hopp Feb 12 '17 at 21:38

1 Answers1

0
System.out.println(Arrays.toString(eliminateDuplicates(list)));
Joe C
  • 15,324
  • 8
  • 38
  • 50
  • 1
    Please [edit] your answer to contain an explanation of how this answers the question, rather than just dropping some code here. – Nic Feb 12 '17 at 23:19