• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

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
0
vihar123
Asked:
vihar123
  • 9
  • 7
  • 7
2 Solutions
 
aozarovCommented:
This is a modified version for the one I showed at: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21399278.html (I though your obj1 are Strings)

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

public String[] replaceString(String[] values)
{
       ArrayList arrayList = new ArrayList(values.length);
       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;
             
               arrayList.add(values[i]);
       }

     return arrayList.toArray(new String[]{});
}
0
 
CEHJCommented:
Set unique = new HashSet();

Now add each element into the set. This will remove duplicates

2. How do you identify the 'specific' element?

0
 
aozarovCommented:
>> 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).
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.

 
aozarovCommented:
>> 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).
0
 
vihar123Author Commented:
>>  return arrayList.toArray(new String[]{});
Incompatible types: found object [] , required string [] .
0
 
CEHJCommented:
>>return arrayList.toArray(new String[]{});

should be


return (String[])arrayList.toArray(new String[]{});
0
 
vihar123Author Commented:
>>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 ?
 
0
 
CEHJCommented:
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((Object[]) 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(innerIter.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(array));
}

0
 
vihar123Author Commented:
>>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.

0
 
aozarovCommented:
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(tmp))
i.remove(); // remove duplicate
else
wasSeenBefore.add(tmp);
}

// Now Vector vec should not have duplicate arrays
0
 
CEHJCommented:
>>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?



0
 
vihar123Author Commented:
>>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
0
 
CEHJCommented:
>>.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 ...
0
 
aozarovCommented:
vihar123 , did you try my second post (4 messages above) for removing duplicates in vec elements?
0
 
vihar123Author Commented:
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

0
 
CEHJCommented:
vihar123 can you explain what was wrong with the code i posted please?
0
 
aozarovCommented:
>> 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..
0
 
vihar123Author Commented:
>> Set  wasSeenBefore = new  HashSet();
>> wasSeenBefore.add(tmp);
Why you are adding it to Set ?
0
 
aozarovCommented:
>> 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.
0
 
CEHJCommented:
>>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
0
 
vihar123Author Commented:
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
0
 
CEHJCommented:
>>My output should be

That was an extra bit of code i put in (possibly influenced by a previous question). The following reduced version gives you the output you want

            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((Object[]) iter.next()));
                  unique.add(temp);
            }

            v.clear();
            iter = unique.iterator();
            while (iter.hasNext()) {
                  List temp = (List) iter.next();
                  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(array));
            }
0
 
CEHJCommented:
I posted working code on this one
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 9
  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now