[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

help needed

Posted on 2005-04-22
16
Medium Priority
?
246 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:vihar123
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13845787
Vector vec // contains some objects
Iterator iter = vec.iterator();
while(iter.hasNext()) {
      if ("100".equals(iter.next())) {
            iter.remove();
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13845855
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);
0
 

Author Comment

by:vihar123
ID: 13845980
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13846034
vec.remove(3); // remove 4th item
0
 

Author Comment

by:vihar123
ID: 13846088
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
0
 

Author Comment

by:vihar123
ID: 13846110
>>vec.remove(3); //  remove 4th item
removes the obj4
but i want to remove the element from obj(i).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13846162
If they're List then you can do:

List list = (List)iter.next();
list.remove(3);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13846183
>>Now ,i want to remove the III element  ...

or in your case:

list.remove(2);
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 13848835
Convert the Object in vector to its corresponding Type (vector, List, Array....) then  remove the particular element U want... !
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13848848
>>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)
0
 

Author Comment

by:vihar123
ID: 13849001
>> List list = (List)iter.next();
Throwing a run time Exception  " java.lang.ClassCastException: java.lang.String " .
0
 

Author Comment

by:vihar123
ID: 13850644
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 " .
0
 
LVL 15

Accepted Solution

by:
aozarov earned 500 total points
ID: 13852583
It seems that your object is a String and not a List.
Strings are immutables so you can't change their content.
You can do something like this:

Vector vec // contains some objects
int length = vec.size();
for (int i = 0; i < length; i++)
{
String obj = (String) vec.get(i);
String newString = replaceString(obj); // add a new method to replace the string
vec.set(i, newString);
}

public String replaceString(String str)
{
       String[] values = str.split(",");
       StringBuffer stBuff = new StringBuffer();
       for (int i  = 0; i < values.length; i++)
       {
                //if you want to remove from the string by index
               if (i == index_to_remove) // change index_to_remove with the right index
                    continue;
               // if you want to remove from string by value
               if (values[i].equals(value_to_remove)) // change value_to_remove with the string to remove
                   continue;
             
               stBuff.append(values[i]).append(",");
       }

       if (stBuff.toString().endsWith(","))
           stBuff.setLength(stBuff.length() - 1);

      return stBuff.toString();
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13855004
>>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'

0
 

Author Comment

by:vihar123
ID: 13866628
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 .



0
 
LVL 15

Expert Comment

by:aozarov
ID: 13868119
:-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question