• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass file into 2d array

 
Bob Holly
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Im new to this and 2d arrays, now i know how to hard code arrays.
[code]
String[][] teamMemberNames = { { "Joe", "Mary", "Allison", "Brian", "Jack" },
{ "Harry", "Pete", "Sam", "Maria", "William", "George", "Dan" }}
[/code}

But now i want to pass a file into an 2darray and display the array to screen.
I read in a csv file using file reader and bufferedreader and then readline() to pass it into a ArrayList of strings.

Normally id convert array list of strings into a string array, use the split() to seperate commas and print the file to a table, but i dont know how to pass a arraylist to a 2darray or use split() in a 2darray.

Any advice?
Bob
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,

Welcome to JavaRanch!

A Java 2D array is actually just an array of 1D arrays; i.e.,



So to build your 2D array, you just allocate the "backbone" and then just fill in the lines. If you don't know the size of the "backbone" until you've created all the lines, then store the lines in a java.util.ArrayList until you've created them all, then use the toArray() method to create the 2D array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic