• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hello a collection problem

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
vivek sivakumar
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This seems to work fine:
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The output is:
     
    I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic