sunshine737
asked on
Questions on vector class
Hello experts ,
Enumeration enum=v.elements(); // v is instance of vector with some objects(string arrays)
while(enum.hasMoreElements ()){
Object obj1=en.nextElement(); // obj1 represents ie Each Object in vector is a " string array "
}
Can Anybody help me to
1)check for the duplicates in vector , if exists remove the duplicate from the vector
2)to remove the specific element from the object ie from the string array.
Thanks
Enumeration enum=v.elements(); // v is instance of vector with some objects(string arrays)
while(enum.hasMoreElements
Object obj1=en.nextElement(); // obj1 represents ie Each Object in vector is a " string array "
}
Can Anybody help me to
1)check for the duplicates in vector , if exists remove the duplicate from the vector
2)to remove the specific element from the object ie from the string array.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> check for the duplicates in vector , if exists remove the duplicate from the vector
Are you looking for duplication String arrays?
If you want to remove duplication string arrays then put the Vector elements inside a TreeSet with a java.util.Comparator that will compare the string array elements.
A simpler aproach is to use an array list of strings instead of String[] (then you don't need to provide the comparator).
Are you looking for duplication String arrays?
If you want to remove duplication string arrays then put the Vector elements inside a TreeSet with a java.util.Comparator that will compare the string array elements.
A simpler aproach is to use an array list of strings instead of String[] (then you don't need to provide the comparator).
>> If you want to remove duplication string arrays then put the Vector elements inside a TreeSet with a java.util.Comparator that will compare the string array elements.
Changed my mine (no need for that).
>> A simpler aproach is to use an array list of strings instead of String[] (then you don't need to provide the comparator).
Still valid to check duplicate using CEHJ HashSet suggestion. (it will not work if you just put String arrays).
Changed my mine (no need for that).
>> A simpler aproach is to use an array list of strings instead of String[] (then you don't need to provide the comparator).
Still valid to check duplicate using CEHJ HashSet suggestion. (it will not work if you just put String arrays).
ASKER
>> return arrayList.toArray(new String[]{});
Incompatible types: found object [] , required string [] .
Incompatible types: found object [] , required string [] .
>>return arrayList.toArray(new String[]{});
should be
return (String[])arrayList.toArra y(new String[]{});
should be
return (String[])arrayList.toArra
ASKER
>>Still valid to check duplicate using CEHJ HashSet suggestion. (it will not work if you just put String arrays).
Then how to delete the duplicates from vector ?
Then how to delete the duplicates from vector ?
You could do something like this:
Vector v = new Vector();
v.add(new String[] { "100", "150", "500" });
v.add(new String[] { "200", "150", "500" });
v.add(new String[] { "100", "150", "500" });
Set unique = new LinkedHashSet();
Iterator iter = v.iterator();
while (iter.hasNext()) {
ArrayList temp = new ArrayList(Arrays.asList((O bject[]) iter.next()));
unique.add(temp);
}
v.clear();
Set toRemove = new HashSet();
toRemove.add("150");
iter = unique.iterator();
while (iter.hasNext()) {
List temp = (List)iter.next();
Iterator innerIter = temp.iterator();
while (innerIter.hasNext()) {
if (toRemove.contains(innerIt er.next()) ) {
innerIter.remove();
}
}
v.add(temp.toArray(new String[]{}));
}
// Now show the results
iter = v.iterator();
while (iter.hasNext()) {
String[] array = (String[])iter.next();
System.out.println(Arrays. toString(a rray));
}
Vector v = new Vector();
v.add(new String[] { "100", "150", "500" });
v.add(new String[] { "200", "150", "500" });
v.add(new String[] { "100", "150", "500" });
Set unique = new LinkedHashSet();
Iterator iter = v.iterator();
while (iter.hasNext()) {
ArrayList temp = new ArrayList(Arrays.asList((O
unique.add(temp);
}
v.clear();
Set toRemove = new HashSet();
toRemove.add("150");
iter = unique.iterator();
while (iter.hasNext()) {
List temp = (List)iter.next();
Iterator innerIter = temp.iterator();
while (innerIter.hasNext()) {
if (toRemove.contains(innerIt
innerIter.remove();
}
}
v.add(temp.toArray(new String[]{}));
}
// Now show the results
iter = v.iterator();
while (iter.hasNext()) {
String[] array = (String[])iter.next();
System.out.println(Arrays.
}
ASKER
>>Vector v = new Vector(); v.add(new String[] 1 { "100", "150", "500" });v.add(new String[] 2 { "200", "150", "500" });
v.add(new String[] 3 { "100", "150", "500" });
My task is to remove the string array ex.string [] 1and string [] 3 are same , now i have to remove the string [] 3 from vector.
v.add(new String[] 3 { "100", "150", "500" });
My task is to remove the string array ex.string [] 1and string [] 3 are same , now i have to remove the string [] 3 from vector.
So I assume the second part (to remove the specific element) is ok,
sorry for forgetting the cast "(String[]) arrayList.toArray(new String[]{});"
Now for the first part you can do something like that:
// assumes you have Vector vec
Set wasSeenBefore = new HashSet();
for (Iterator i = vec.iterator(); i.hasNext(); ) // use Iterator instead of Enumeration because we are going to use i.remove();
{
String[] strings = (String[]) i.next();
List tmp = Arrays.asList(strings);
if (wasSeenBefore.contains(tm p))
i.remove(); // remove duplicate
else
wasSeenBefore.add(tmp);
}
// Now Vector vec should not have duplicate arrays
sorry for forgetting the cast "(String[]) arrayList.toArray(new String[]{});"
Now for the first part you can do something like that:
// assumes you have Vector vec
Set wasSeenBefore = new HashSet();
for (Iterator i = vec.iterator(); i.hasNext(); ) // use Iterator instead of Enumeration because we are going to use i.remove();
{
String[] strings = (String[]) i.next();
List tmp = Arrays.asList(strings);
if (wasSeenBefore.contains(tm
i.remove(); // remove duplicate
else
wasSeenBefore.add(tmp);
}
// Now Vector vec should not have duplicate arrays
>>My task is to remove the string array ex.string [] 1and string [] 3 are same
The code i posted does that
>>now i have to remove the string [] 3 from vector.
This is what you asked earlier:
>>2)to remove the specific element from the object ie from the string array.
Now you seem to be saying you want to remove the whole array instead of a 'specific element' from an array. Which is it?
The code i posted does that
>>now i have to remove the string [] 3 from vector.
This is what you asked earlier:
>>2)to remove the specific element from the object ie from the string array.
Now you seem to be saying you want to remove the whole array instead of a 'specific element' from an array. Which is it?
ASKER
>>Now you seem to be saying you want to remove the whole array instead of a 'specific element' from an array. Which is it?
aozarov has already posted the code to remove a specific element from the string array ie aozarov's first post in this thread .And I Have already mentioned that i have two Q's .
Thanks
aozarov has already posted the code to remove a specific element from the string array ie aozarov's first post in this thread .And I Have already mentioned that i have two Q's .
Thanks
>>.And I Have already mentioned that i have two Q's .
Yes, i can see from the numbering at your first post. The question is, what's the second one? My last posted code seems to do exactly what you want (did you run it?), yet you now imply
>> now i have to remove the string [] 3 from vector.
that you want to remove the whole array from the vector rather than an element of it ...
Yes, i can see from the numbering at your first post. The question is, what's the second one? My last posted code seems to do exactly what you want (did you run it?), yet you now imply
>> now i have to remove the string [] 3 from vector.
that you want to remove the whole array from the vector rather than an element of it ...
vihar123 , did you try my second post (4 messages above) for removing duplicates in vec elements?
ASKER
Hi aozarov
>> vihar123 , did you try my second post (4 messages above) for removing duplicates in vec elements?
I tried it , its working , but is there any other way to remove the duplicates , because here the objects in the vector are adding to set after checking for duplicates and again i have to add them to vector. My Final data should be in vector.
Thanks
>> vihar123 , did you try my second post (4 messages above) for removing duplicates in vec elements?
I tried it , its working , but is there any other way to remove the duplicates , because here the objects in the vector are adding to set after checking for duplicates and again i have to add them to vector. My Final data should be in vector.
Thanks
vihar123 can you explain what was wrong with the code i posted please?
>> because here the objects in the vector are adding to set after checking for duplicates and again i have to add them to vector.
Not sure what you mean, this check removes duplicates from the Vector without the need to add them again..
Not sure what you mean, this check removes duplicates from the Vector without the need to add them again..
ASKER
>> Set wasSeenBefore = new HashSet();
>> wasSeenBefore.add(tmp);
Why you are adding it to Set ?
>> wasSeenBefore.add(tmp);
Why you are adding it to Set ?
>> Why you are adding it to Set ?
That operation itself does not change the original vec vector.
I do that in order to detect duplicates.
I am iterating all the items in the vector vec.
for each item in vec I check if is this item already exists .
That is done by an auxiliary set (which can be discarded after that loop).
The idea behind it to add items to wasSeenBefore as long as they are not there (original items) and if they are there (duplicates) we remove them from vec.
That operation itself does not change the original vec vector.
I do that in order to detect duplicates.
I am iterating all the items in the vector vec.
for each item in vec I check if is this item already exists .
That is done by an auxiliary set (which can be discarded after that loop).
The idea behind it to add items to wasSeenBefore as long as they are not there (original items) and if they are there (duplicates) we remove them from vec.
>>I do that in order to detect duplicates.
IOW, for exactly the same reason i used the same approach earlier. Given starting duplicates, and the 'need' to remove 150 the output of the code i posted is:
[100, 500]
[200, 500]
btw please don't ignore my questions vihar123 - it's very rude
IOW, for exactly the same reason i used the same approach earlier. Given starting duplicates, and the 'need' to remove 150 the output of the code i posted is:
[100, 500]
[200, 500]
btw please don't ignore my questions vihar123 - it's very rude
ASKER
Hi CEHJ,
Iam sorry if i hurted you.But For my problem aozarov's code works perfectly.
About your code, it doesn't works for me , because
v.add(new String[] { "100", "150", "500" });
v.add(new String[] { "200", "150", "500" });
v.add(new String[] { "100", "150", "500" });
My output should be
[100, 150, 500]
[200, 150, 500]
Sorry Once Again
Iam sorry if i hurted you.But For my problem aozarov's code works perfectly.
About your code, it doesn't works for me , because
v.add(new String[] { "100", "150", "500" });
v.add(new String[] { "200", "150", "500" });
v.add(new String[] { "100", "150", "500" });
My output should be
[100, 150, 500]
[200, 150, 500]
Sorry Once Again
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I posted working code on this one
Now add each element into the set. This will remove duplicates
2. How do you identify the 'specific' element?