sunshine737
asked on
help needed
Hello Experts,
Vector vec // contains some objects
Enumeration en=vec.elements();
while(en.hasMoreElements() ){
Object obj=en.nextElement();
}
now the "obj" contains some values like 100 200 300 400...
Can you give me the code to remove a specific value from "obj".
Thanks
Vector vec // contains some objects
Enumeration en=vec.elements();
while(en.hasMoreElements()
Object obj=en.nextElement();
}
now the "obj" contains some values like 100 200 300 400...
Can you give me the code to remove a specific value from "obj".
Thanks
If you want to check a number of values, you can do:
Vector vec = new Vector(); // contains some objects
vec.add("150");
vec.add("100");
vec.add("400");
System.out.println(vec);
Iterator iter = vec.iterator();
Set toRemove = new HashSet(Arrays.asList(new String[] { "100", "150", "500" }));
while(iter.hasNext()) {
if (toRemove.contains(iter.ne xt())) {
iter.remove();
}
}
System.out.println(vec);
Vector vec = new Vector(); // contains some objects
vec.add("150");
vec.add("100");
vec.add("400");
System.out.println(vec);
Iterator iter = vec.iterator();
Set toRemove = new HashSet(Arrays.asList(new String[] { "100", "150", "500" }));
while(iter.hasNext()) {
if (toRemove.contains(iter.ne
iter.remove();
}
}
System.out.println(vec);
ASKER
Iam not removing the duplicates , I should be able to remove particular element like element at 3 , so it should remove 300 from the obj of my vector.
vec.remove(3); // remove 4th item
ASKER
to say in detail
Object obj=en.nextElement(); // returns objects like obj1,obj2,obj3.......
I II III IV
obj1 contains 100 200 300 400
obj2 contains 500 600 700 800
obj3 contains 400 600 200 300
Now ,i want to remove the III element of each object ie 300 of obj1,700 of obj2 ,200 of obj3
Thanks
Object obj=en.nextElement(); // returns objects like obj1,obj2,obj3.......
I II III IV
obj1 contains 100 200 300 400
obj2 contains 500 600 700 800
obj3 contains 400 600 200 300
Now ,i want to remove the III element of each object ie 300 of obj1,700 of obj2 ,200 of obj3
Thanks
ASKER
>>vec.remove(3); // remove 4th item
removes the obj4
but i want to remove the element from obj(i).
removes the obj4
but i want to remove the element from obj(i).
If they're List then you can do:
List list = (List)iter.next();
list.remove(3);
List list = (List)iter.next();
list.remove(3);
>>Now ,i want to remove the III element ...
or in your case:
list.remove(2);
or in your case:
list.remove(2);
Convert the Object in vector to its corresponding Type (vector, List, Array....) then remove the particular element U want... !
>>Convert the Object in vector to its corresponding Type (vector, List, Array....) then remove the particular element U want... !
(Already posted an example of that)
(Already posted an example of that)
ASKER
>> List list = (List)iter.next();
Throwing a run time Exception " java.lang.ClassCastExcepti on: java.lang.String " .
Throwing a run time Exception " java.lang.ClassCastExcepti
ASKER
Object obj=en.nextElement(); // returns objects like obj1,obj2,obj3
And obj1 contains different data elements like Strings , Int ,float ,and Date.....
If i convert the Object in vector to its corresponding Type (vector, List, Array....) then its Throwing a run time Exception " java.lang.ClassCastExcepti on: java.lang.String " .
And obj1 contains different data elements like Strings , Int ,float ,and Date.....
If i convert the Object in vector to its corresponding Type (vector, List, Array....) then its Throwing a run time Exception " java.lang.ClassCastExcepti
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Throwing a run time Exception " java.lang.ClassCastExcepti on: java.lang.String " .
If you don't have List as the type in the vector then just use the approach i suggested at first:
>>
if (toRemove.contains(iter.ne xt())) {
iter.remove();
}
>>
If you have objects of various types in there, you just have to assign the correct type and value to 'toRemove'
If you don't have List as the type in the vector then just use the approach i suggested at first:
>>
if (toRemove.contains(iter.ne
iter.remove();
}
>>
If you have objects of various types in there, you just have to assign the correct type and value to 'toRemove'
ASKER
Hi,
Thanks for your help.
aozarov 's post is the exact solution for my Question , So iam awarding points to aozarov .
Thanks once again aozarov .
Thanks for your help.
aozarov 's post is the exact solution for my Question , So iam awarding points to aozarov .
Thanks once again aozarov .
:-)
Iterator iter = vec.iterator();
while(iter.hasNext()) {
if ("100".equals(iter.next())
iter.remove();
}
}