0

Sorry to Ask this question Again i had found alot in forum.But as of new to java am Excepting with Explanation code i tried in my way but it not working .i tried without any default method the code have to work for this program

Code:

    package javatest;

public class Test03sep {

    public static void main(String args[]) 
    {
                        //0   1            2    3
        String []val={"amma","senthil","kumar","amma"};
        removeduplicate(val);
    }
    static void removeduplicate(final String []arr)
    {

        String temp="";
             for (int i = 0; i < arr.length; i++) 
             {
            for (int j = i+1; j < arr.length; j++) 
             {
                System.out.print("condition are:");
                 System.out.println(arr[i]==arr[j]);

                 if(arr[i]==arr[j])
                     {
                     temp=arr[i];
                     arr[i]=arr[j];
                     arr[j]=temp;

                     }

        }
            for (String string : arr) {
                System.out.println("string array are==>"+string);
            }
}
    }
}

output:

condition are:false
condition are:false
condition are:true
string array are==>amma
string array are==>senthil
string array are==>kumar
string array are==>amma
condition are:false
condition are:false
string array are==>amma
string array are==>senthil
string array are==>kumar
string array are==>amma
condition are:false
string array are==>amma
string array are==>senthil
string array are==>kumar
string array are==>amma
string array are==>amma
string array are==>senthil
string array are==>kumar
string array are==>amma

But i am Excepting the output are String array are===>{"amma","senthil","kumar"};

could some one can guide on this please????

user3607180
  • 185
  • 2
  • 5
  • 14
  • `for (String string : arr) { System.out.println("string array are==>"+string); }` If you are using an IDE, try to debug this code and see what happens when it executes the above code. – Praba Sep 10 '14 at 13:29
  • Just FYI, if you want to exclusively work with items without duplicates, you could look at java `Set` class. – vikingsteve Sep 10 '14 at 13:33
  • @vikingsteve my goal here to achieve without use a Default method like equals,contain,set,treeset...etc ...how to avoid and over come for my solution – user3607180 Sep 10 '14 at 13:41

3 Answers3

0

Please change if(arr[i]==arr[j]) to if(arr[i].equals(arr[j]))

While comparing string objects you should use equals() method instead of "=="

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

This is not correct as this is doing a memory comparison :

                 if(arr[i]==arr[j])
                 {
                   temp=arr[i];
                   arr[i]=arr[j];
                   arr[j]=temp;
                 }

Change it too this code to do value comparison:

                 if(arr[i].equals(arr[j]))
                 {
                   temp=arr[i];
                   arr[i]=arr[j];
                   arr[j]=temp;

                 }
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
0

if(arr[i]==arr[j]) replace to if(arr[i].equals(arr[j])) because == match hashcode of objects while equals() method is overrided in string and it match string char by char so use equals() methos instead of == operator

Amit Kumar
  • 547
  • 3
  • 10