| Author |
Hello a collection problem
|
vivek sivakumar
Ranch Hand
Joined: Aug 09, 2001
Posts: 187
|
|
|
I am having a collection which returns something like this [1000,2000,30000] this is a collcetion and i try to use vector.addAll(Collection) what happens is that the entire stuff is added like [1000,2000,30000] this itself but i want to add only the elements inside this collection to the vactor how can i do it? will the addElement() function work or addAll(collection) work ? with addAll(collection) my vector contents is like this [1000,2000,3000], [] , but what i need is that it should be like this [1000,2000,3000,..if i have something more] , how can i take out [] value from inside my vector.? Thanks for ur help
|
SCJP, SCWD <br />A farmer learns more from a bad harvest than a good one.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Perhaps I'm not understanding your problem(s)/question(s) correctly. Vector::addAll(Collection) seems to me to do just what you want to do: So, what am I missing?
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
vivek sivakumar
Ranch Hand
Joined: Aug 09, 2001
Posts: 187
|
|
sorry for the poor explanation fo my problem , to be elaborative , well in my program i have vector defined like this Vector tokenvector = new Vector(); this tokenvactor already has a value 12 inside i have a method like this public Collection getPattern(.....) this methods searches for a particular pattern in a string and then returns me the values of the patterns like 1000,2000,3000 (just assume that these are just numbers and nothing else) these values im getting them as collection(as defined by me in my method) i want to addthem to my vector tokenvector, and if i use vectors addAll(collection) then no problem my values are added but something like this [2000,3000],[12] but i want it to be like [2000,3000,12] together how can i do it thats my problem. Another problem is that my vector has some values like []how can i remove them i did use tokenValuesVector.removeAll(Collections.singleton(null)); but nothing works
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Is it possible to post an example Collection and an example Vector that replicates your problem? I can't replicate the problem with the examples provided. These are the examples as I've understood them: I'm assuming that the order of the vector components isn't the focus of concern.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Ok, I'll confess. I can replicate your problem (kind of & sort of), but not with the data structures provided (not exactly at least). Consider: Now, the obvious problem is that the Collection is a Collection of dissimilar objects - String objects and another Collection object (and Se�or null - just to see what he would do). So, is this more your situation?
|
 |
vivek sivakumar
Ranch Hand
Joined: Aug 09, 2001
Posts: 187
|
|
Exactly i think this is my situation , a snapshot of my code(if u did not see my email that i sent) a method like this public ArrayList getPat(); ArrayList myarr = new ArrayList(); myarr = getPat(); Vactor myvect = new Vector(); myvect.addAll(myarr); this is what i did and i think its almost similar to scenario posted by you ... pls help further
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
I don't see any option other than to investigate each and every component in the incoming Collection for what type of object it is. The test for each component could be along the lines: If it is not a Collection object, then add it to the Vector.If it is a Collection, then add the components of the Collection to the Vector. Of course, if you have Collections buried within Collections within Collections, recursion might be useful (however not very object oriented - so you might want to do it differently). I doubt that the standard API has a "built in" method that would do this for you. Do you have any ideas on where to begin?
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
This seems to work fine:
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
The output is:
|
 |
 |
|
|
subject: Hello a collection problem
|
|
|