Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Arrays of strings from a Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Arrays of strings from a Vector " Watch "Arrays of strings from a Vector " New topic
Author

Arrays of strings from a Vector

Neeraj Sharma
Greenhorn

Joined: Apr 24, 2003
Posts: 5
Hi all,
Can anybody tell me how to construct arrays of strings from a vector dynamically?
I have a vector which has data in this form. /aaa /bbb /ccc are the indicators and the data follows them.
/aaa
xxx
xxx
xxx
xxx
xxx
xxx
/bbb
xxx
xxx
xxx
/ccc ....
/ccc ...
I want to dynamically shift /aaa contents into a separate array (say aaa). Similarly /bbb strings into bbb array. There is only one element (/ccc) which can repeat multiple times. In such a case how do we construct multiple arrays?
Thanks in advance.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sounds like this is a bit too complex to do in one step. The basic problem is that for an array, you really need to know how many elements there are before you create the array. (Or you can write code to resize the array automatically, but that's more complex, and that's what ArrayList does anyway, so why reinvent the wheel?) I would recommed that you first create several different ArrayLists, one for each array you plan to have eventually. E.g. aList, bList, cList... Then loop through the initial ArrayList to look at eack element and decide which of the other lists that line should be copied to. At this stage, you don't know how many elements will go in a, b, c, etc., so it's easier to just use ArrayLists while you're still deciding this. Then when you're done, you can convert each ArrayList to a String[]:


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays of strings from a Vector