Link to home
Start Free TrialLog in
Avatar of sunshine737
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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Vector vec // contains some objects
Iterator iter = vec.iterator();
while(iter.hasNext()) {
      if ("100".equals(iter.next())) {
            iter.remove();
      }
}
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.next())) {
                              iter.remove();
                        }
                  }
                  System.out.println(vec);
Avatar of sunshine737
sunshine737

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
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
>>vec.remove(3); //  remove 4th item
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);
>>Now ,i want to remove the III element  ...

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)
>> List list = (List)iter.next();
Throwing a run time Exception  " java.lang.ClassCastException: java.lang.String " .
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.ClassCastException: java.lang.String " .
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Throwing a run time Exception  " java.lang.ClassCastException: 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.next())) {
                         iter.remove();
                    }
>>

If you have objects of various types in there, you just have to assign the correct type and value to 'toRemove'

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 .



:-)