1

I'm trying to create a 2D array from a .txt file, where the .txt file looks something like this:

xxxx            
xxxx                 
xxxx                 
xxxx                

or something like this:

xxx
xxx
xxx

So I need to handle multiple sizes of a 2D array (Note: Each 2D array will not always be equal x and y dimensions). Is there anyway to initialize the array, or get the number of characters/letters/numbers per line and number of columns? I do not want to use a general statement, something like:

String[][] myArray = new Array[100][100];

And then would filling the array using filewriter and scanner classes look like this?

File f = new File(filename);
Scanner input = new Scanner(f);
for(int i = 0; i < myArray[0][].length; i++){
  for(int j = 0; j < myArray[][0].length, j++){
    myArray[i][j] = input.nextLine();
  }
}
user3413170
  • 251
  • 1
  • 3
  • 13
  • 2
    Myself, I'd use a List of List, say `List>`, and likely instantiate my Lists as ArrayLists. Is this not allowed? – Hovercraft Full Of Eels Jan 31 '15 at 22:18
  • Ideally I'd like to just stick to 2D arrays and shy away from arraylists – user3413170 Jan 31 '15 at 22:20
  • why 2D array ? Why not 1D array of String and let the string handle the variable length per line ? Or why not List ? – anu Jan 31 '15 at 22:26
  • The way I want to program to run is with a 2D array, because I'm more familiar with it. – user3413170 Jan 31 '15 at 22:30
  • 1
    You owe it to yourself to gain familiarity with ArrayLists and similar collections. You won't regret doing this, believe me as it will add much flexibility and power to your code. – Hovercraft Full Of Eels Jan 31 '15 at 22:31
  • A list is like an array, except it's better because it's dynamic in size – EpicPandaForce Jan 31 '15 at 22:33
  • I understand that much, but the point at which I understand Java right now, I think trying to use an arraylist would confuse me. I have past experience with arrays, but not arraylists. – user3413170 Jan 31 '15 at 22:35
  • @user3413170 That's not a good reason. Strings and List classes provide abstraction and ease that's not available in vanilla C and it helps write code much effectively in a shorter amount of time. It's totally worth it to learn it. Also, don't try to fit C paradigm into Java. – anu Jan 31 '15 at 22:36
  • @anu I have no past experience with C. My arraylist knowledge is very limited, and I believe it would be much easier for me to complete the task with a 2D array. – user3413170 Jan 31 '15 at 22:45
  • @user3413170: then iterate through your file twice if you must. – Hovercraft Full Of Eels Jan 31 '15 at 22:52

2 Answers2

2

You have several choices as I see it:

  1. Iterate through the file twice, the first time getting the array parameters, or
  2. Iterate through it once, but fill up a List<List<SomeType>> possibly instantiating your Lists as ArrayLists. The latter will give you much greater flexibility in the short and long run.
  3. (per MadProgrammer) The third option is to re-structure the file to provide the meta data required to make decisions about the size of the array.

For example, using your code,

  File f = new File(filename);
  Scanner input = new Scanner(f);
  List<List<String>> nestedLists = new ArrayList<>();
  while (input.hasNextLine()) {
     String line = input.nextLine();
     List<String> innerList = new ArrayList<>();
     Scanner innerScanner = new Scanner(line);
     while (innerScanner.hasNext()) {
        innerList.add(innerScanner.next());
     }
     nestedLists.add(innerList);
     innerScanner.close();
  }
  input.close();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Java Matrix can have each line (which is an array) by your size desicion.

You can use: ArrayUtils.add(char[] array, char element) //static method But before that, you need to check what it the file lines length

Either this, you can also use ArrayList> as a collection which is holding your data

roeygol
  • 4,908
  • 9
  • 51
  • 88