• Post Reply Bookmark Topic Watch Topic
  • New Topic

a better way to convert List to array?  RSS feed

 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!