469,282 Members | 1,749 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Delete duplicates array (String)

11
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
Aug 5 '07 #1
4 13544
JosAH
11,448 Expert 8TB
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
BeanShell is great fun to work/play with; as far as I know you can't use generics
yet so you have to do this (for arrays of Objects of any kind)

Expand|Select|Wrap|Line Numbers
  1. Set set= new HashSet(Arrays.asList(yourArray));
  2.  
The set contains all your elements from yourArray but only once.

kind regards,

Jos
Aug 5 '07 #2
Mokita
11
BeanShell is great fun to work/play with; as far as I know you can't use generics
yet so you have to do this (for arrays of Objects of any kind)

Expand|Select|Wrap|Line Numbers
  1. Set set= new HashSet(Arrays.asList(yourArray));
  2.  
The set contains all your elements from yourArray but only once.

kind regards,

Jos

Thank you very much, it was very helpful.

Mokita
Aug 5 '07 #3
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.
Mar 20 '08 #4
JosAH
11,448 Expert 8TB
Reply:-

HI,
Try the following logic for String array...........
Did you read reply #2? (eight months ago).

kind regards,

Jos
Mar 20 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Alexander Anderson | last post: by
3 posts views Thread by Quack Boy | last post: by
3 posts views Thread by _eddie_ | last post: by
1 post views Thread by jmarcrum | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.