This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes a better way to convert List to array? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "a better way to convert List to array?" Watch "a better way to convert List to array?" New topic
Author

a better way to convert List to array?

Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 140
I convert an ArrayList String to array string. Please give me some tips if you have how to handle a better way to have array string instead of typing
45 times as below.



ArrayList<String> al = getIngestor();
Object ia[] = al.toArray();



Object [][] rowData = {

{ ia[0].toString(), ia[1].toString(), ia[2].toString(),ia[3].toString(),ia[4].toString()},
{ ia[5].toString(), ia[6].toString(), ia[7].toString(),ia[8].toString(),ia[9].toString()},
{ ia[10].toString(), ia[11].toString(), ia[12].toString(),ia[13].toString(),ia[14].toString()},
{ ia[15].toString(), ia[16].toString(), ia[17].toString(),ia[18].toString(),ia[19].toString()},
{ ia[20].toString(), ia[21].toString(), ia[22].toString(),ia[23].toString(),ia[24].toString()},
{ ia[25].toString(), ia[26].toString(), ia[27].toString(),ia[28].toString(),ia[29].toString()},
{ ia[30].toString(), ia[31].toString(), ia[32].toString(),ia[33].toString(),ia[34].toString()},
{ ia[35].toString(), ia[36].toString(), ia[37].toString(),ia[38].toString(),ia[39].toString()},
{ ia[40].toString(), ia[41].toString(), ia[42].toString(),ia[43].toString(),ia[44].toString()}


};
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9950
    
    6

I'm not sure I'd do it at all, but why can't you loop through it? You know the size you'll need, and just increment x by 5.


Never ascribe to malice that which can be adequately explained by stupidity.
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 140
fred rosenberger wrote:I'm not sure I'd do it at all, but why can't you loop through it? You know the size you'll need, and just increment x by 5.


I have been thinking about using loop, but how ?
Can you please show me a piece of code ?
Thank you.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

Your rowData is an array of arrays that contain Objects. You'll need two nested for-loops for this. One that loops over the outer index and one that loops over the inner index. The inner loop needs to iterate 5 times, since each array element contains an array of 5 elements, according to your example.

Please try writing something yourself and show it here if you have trouble getting it right.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: a better way to convert List to array?
 
Similar Threads
String array to arraylist
Conversion of Vector() to string
Help on Drop Down
Practical Number?
convert an "ArrayList of Strings" into String[] ?