0

Possible Duplicate:
How do I compare strings in Java?

I have written this code:

    public String[] removeDuplicates(String[] input){

    int i;
    int j;
    int dups = 0;
    int array_length = input.length;
    for(i=0; i < array_length; i++){
        //check whether it occurs more than once
        for(j=0; j < array_length; j++){
            if (input[i] == input[j] && i != j){
                dups++; //set duplicates boolean true
                input[j] = null;  //remove second occurence
            } //if cond
        } // for j
    } // for i
    System.out.println("Category contained " + dups + " duplicates.");
    return input;
}

which is supposed to check whether an array of strings contains one or more duplicates. However, even when I define the array like this:

String[] temp = new String[2];
temp[0] = "a";
temp[1] = "a"; 

The if condition is not "triggered". Did I misunderstand how && works? In my opinion, the program should first check whether the two strings are identical (which they are...) and then whether the two indices are the same. If not, it should perform the operations. However, the programs seems to think otherwise.

Community
  • 1
  • 1
padrino
  • 299
  • 3
  • 14
  • 1
    Not directly related, but you could speed this up by initializing j = i, since you obviously don't need to retest the lower strings. You should also check whether input[i] is null before comparing... – sybkar Sep 20 '12 at 15:06
  • 1
    You can even avoid checking if `i!=j`. You just initialize `j` to be `i+1`. So the second for loop looks like: `for(j=i+1; j < array_length; j++){...}` – Nejc Sep 20 '12 at 15:22

5 Answers5

5

One of the most common mistakes in Java is to assume that a String is an object when its a reference to an object. When you use == you are comparing references, not their contents. This is why .equals() is required to compare their contents.

BTW you can remove duplicates with

public static String[] removeDuplicates(String[] input){
    return new HashSet<String>(Arrays.asList(input)).toArray(new String[0]);
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4

The == operator in Java checks if the two objects are the same, not that they are equal. Two strings may have identical content, and compare negatively for equality. You need to use equals instead:

if (i != j && input[i].equals(input[j])){
}

If null values are allowed among the input elements, you need to add a null check to your condition to avoid an exception:

if (i != j && input[i] != null && input[i].equals(input[j])){
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Never use == to check that two objects have the same value. Use equals()

== will check their memory positions (if both objects are in fact only one), equals() is the method that will tell you if both represent the same information.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
2

While working with Strings (and any non-primitive type), remember that == makes a comparation by reference, not by value. Use equals() instead.

if (input[i].equals(input[j]) && i != j){
    dups++; //set duplicates boolean true
    input[j] = null;  //remove second occurence
} //if cond

As a rule of thumb, use == when you want to check if two objects are EXACTLY the same object (you can think of it as if both pointers where referencing the same address).

Fritz
  • 9,987
  • 4
  • 30
  • 49
1

You should use String.equals for checking string content. The == operator just checks the object reference:

if (input[i] != null && input[i].equals(input[j]) && i != j) {
Reimeus
  • 158,255
  • 15
  • 216
  • 276