Quote:
Originally Posted by Mokita
Hello,
I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String).
for (int k=0; k<myLength; k++){
boolean isthere=false;
for (int l=0; l<sizeres; l++){
if (res[l].equals(my[k])){
isthere=true;
break;
}
}
if (isthere == false){
res[free]= my[k];
temp.append(res[free] + "\n");
free++;
}
}
The beanshell gives me an error in the method .equals(my[k]).
Has anyone work with beanshell already?
Does anyone has an idea how to delete duplicates from an array of String in a easier way?
I also thought about java.util.Set, but it not working too.
Thank you in advance,
Mokita
Reply:-
HI,
Try the following logic for String array...........
private static void removeDuplicatesFromArray(){
int exampleArray [] = {4,5,1,1,2,3,2,4,5,1,2,0};// Example Array!
int exampleArrayLength = exampleArray.length;
int searchArray [] = new int[exampleArrayLength]; // Empty Array used to remove duplicates from Example Array
int searchArrayLength=1;
//I now want to print the above array without any duplicate values, which is my problem!
for(int x = 0; x < exampleArrayLength; x++){
if(x==0){
searchArray[0] = exampleArray[0];
}else{
int elementFound=0;
for(int y=0;y<searchArrayLength;y++){
if(exampleArray[x] != searchArray[y]){
elementFound=0;
}else{
elementFound=elementFound+1;
break;
}
}
if(elementFound==0){
searchArray[searchArrayLength] = exampleArray[x];
searchArrayLength++;
}
}
}
int outArray[]=new int[searchArrayLength];
System.arraycopy(searchArray,0,outArray,0,searchAr rayLength);
for(int x = 0; x < outArray.length; x++){
System.out.println(outArray[x]);
}
}
Regards,
Karthik.