0

I read this thread, but, how can I put text from a file dynamically into multidimensional array?

Data like:

1,4,5,7,nine

11,19

22,twenty-three,39

30,32,38
..
..
Community
  • 1
  • 1
Kapil M
  • 63
  • 1
  • 9

2 Answers2

1

You could do something like:

try {
    ArrayList<List<String> > matrix = new ArrayList<List<String> >();
    FileInputStrem iStream = new FileInputStream("path/to/file");
    BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
    String line = reader.readLine();
    while(line != null){
        line = reader.readLine();
        matrix.add(Arrays.asList(line.split(",")));
    }
} catch (Exception e) {
    // handle exception
} finally {
     reader.close();
     istream.close();
}

One of the biggest advantage of ArrayList over array is that ArrayList is dynamic in size and it is amortized constant to add to the end.

To answer your comment, to remove:

for (int i = 0; i < matrix.size(); i++) {
    for (int j = 0; j < matrix.get(i).size(); j++) {
        if (someCondition) // determine which to remove
            matrix.get(i).remove(j);
    }
}
Zac Lozano
  • 778
  • 1
  • 4
  • 12
  • Ok.Just got the answer here.What i was doing was List myList = new ArrayList(); and this ArrayList> myList = new ArrayList>(); couldn't got it right. and using your declaration.worked. matrix.get(i).get(j). if not bothered, can you tell me what I was wrong? – Kapil M Jun 07 '14 at 02:18
  • Your question was slightly unclear. Were you getting a compile-time error, or a run-time exception? – Zac Lozano Jun 07 '14 at 23:53
  • sorry about that. \n It was run-time error for try and catch. But, your method works now, as I can get items with `matrix.get(i).get(j)`. \n I cannot return to the past with no backup :(. \n but, Good thing is your method works good. \n Can you tell me how to delete specific items. `for (int j1 = mylist.get(i).size()-1; j1 > mylist.get(i).size()-2; j1--) {` `mylist.remove(mylist.get(0).get(j1));` `}` – Kapil M Jun 08 '14 at 14:28
  • I answered above for readability. – Zac Lozano Jun 09 '14 at 14:43
  • I am getting a null error on removing – Kapil M Jun 11 '14 at 07:31
  • I had my removal on here.Thanx @Zac . http://stackoverflow.com/questions/24122565/android-remove-items-from-arraylistliststring – Kapil M Jun 14 '14 at 01:51
1

Use a class such as BufferedReader to read each line from the file. Use a scanner on each line to get the individual tokens.

Scanner scanner = new Scanner(currentLineInFile);
scanner.useDelimiter(",");

while (scanner.hasNext())
{
    String token = scanner.next(); // Only call this once in the loop.
    if (token.isEmpty())
    {
        // This case trips when the string "this,is,,a,test" is given.
        // If you don't want empty elements in your array, this is where
        // you need to handle the error.
        continue; // or throw exception, Log.d, etc.
    }
    // Do something with the token (aka. add it to your array structure.
}

If you couple this to the array structure will allow you to handle data on demand (like from a web service.) This implementation accounts for cases with two ',' next to each other. You can handle these empty elements as you read them. Using String.split will leave these empty elements in the array which you'll have to handle later.

sotrh
  • 386
  • 5
  • 13