Reputation: 69
I am trying to read in a file. I want to store the contents of the file in a 2D arraylist, where each place in the list holds one character. I want it so that each line in the original text file is one row in the array list. For instance if the input from the text file was:
a,b,c
d,e,f
g,h,i
then the array would hopefully look like: {[a,b,c],[d,e,f],[g,h,i]}. Unfortunately at the moment my outcome is just [a,b,c,d,e,f,g,h,i]. Any ideas would be appreciated, I am new to Java. Thanks.
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Coursework1
{
public static void main(String[] args)
{
try {
List<String> list = new ArrayList<String>();
String thisLine = null;
BufferedReader br;
br = new BufferedReader(new FileReader("map.txt"));
while ((thisLine = br.readLine()) != null) {
list.add(thisLine);
}
char[][] firstDimension = new char[list.size()][];
for (int i = 0; i < list.size(); i++) {
firstDimension[i] = list.get(i).toCharArray();
}
for (int i=0;i<firstDimension.length;i++) {
for (int j=0;j<firstDimension[i].length;j++) {
System.out.println(firstDimension[i][j]);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 7048
Reputation: 210
Each line (String) is already a list of characters with methods that let you iterate over it (length, charAt). So depending on what you are trying to achieve a list of strings may be enough.
Upvotes: 0
Reputation: 4350
You can embed Lists within Lists like:
List<List<String>> matrix = new ArrayList<List<String>>();
Then accessing elements you'd do,
matrix.get(1).get(2);
EDIT: I would also suggest you think more about if you want an Array or an ArrayList. They're not the same. An array list is resizable (you can keep appending more and more elements onto it, you're not forced to only have so many elements in the array list. This allows you to do things like have a List<List<String>>
object where not every second dimension is the same length. Arrays on the other hand have a specific size associated with them, which would enable you to enforce that all rows have the same length, and all columns have the same height...
Upvotes: 1
Reputation: 15834
It seems that you don't fully understand the difference between an array and an array list.
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. So you have to know the size before you create it or you have to create it of a sufficient size.
On the other hand, ArrayList is a collection (complex type, see this tutorial for more information) which can hold simple objects, collections,... ArrayList can be imagined as a dynamic array - you don't have to know the initial size, it is automatically resized if you add or remove elements from it.
It is a good habit to iterate over the ArrayList like this
List<String> names = new ArrayList<String>();
// add something
// foreach loop
for (String name : names) {
// here you have access directly to the value
// and you don't have to write list.get(index)
System.out.println(name);
}
Now to you question. If you read the link above you should be able to construct it by yourself. But if you still don't know:
List<List<String>> twoDimensionList = new ArrayList<List<String>>();
Iteration with foreach loop
for (List<String> innerList : twoDimensionList) {
for (String value : innerList) {
...
}
}
Upvotes: 2
Reputation: 42597
Your code works, you're just printing out the results wrong. Try:
for (int i = 0; i < firstDimension.length; i++)
{
for (int j = 0; j < firstDimension[i].length; j++)
{
System.out.print(firstDimension[i][j]);
}
System.out.println();
}
This prints the chars from the same line on, well, the same line...
Upvotes: 0
Reputation: 123
your answer is actually correct and fully functioning. Your test code is just wrong as it puts too many new lines in.
for (int i=0;i<firstDimension.length;i++)
{
System.out.println();
for (int j=0;j<firstDimension[i].length;j++)
{
System.out.print(firstDimension[i][j]);
}
}
Upvotes: 2