trouble with array duplicate filter android/java
I have a string array witch could contain some duplicate values and I am trying to write a filter to eliminate any dups.
The code I pasted below works for all but the first element in the array and I cant figure out why.
Thanks for any help you can offer.
for(int i=0; i<forparts.length; i++){
elem = forparts[i];
for (int n=i+1; n<forparts.length; n++){
elem2 = forparts[n];
if (开发者_开发知识库elem2.equalsIgnoreCase(elem)){
forparts[n] = "";
}
}
}
Thinking outside the box, must you use an array? The behavior you need is available from the Set interface in the Java Collections library.
Set<MyType> set = new LinkedHashSet<MyType>();
MyType obj = new MyType();
boolean bAdded;
bAdded = set.add(obj); // bAdded == true, set.size() == 1
bAdded = set.add(obj); //bAdded == false; set.size() == 1
This way you never need to filter dupes from your collection because they're never added in the first place.
精彩评论