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();
  }
}
10
  • 2
    Myself, I'd use a List of List, say List<List<SomeType>>, and likely instantiate my Lists as ArrayLists. Is this not allowed? Commented Jan 31, 2015 at 22:18
  • Ideally I'd like to just stick to 2D arrays and shy away from arraylists Commented Jan 31, 2015 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<String> ? Commented Jan 31, 2015 at 22:26
  • The way I want to program to run is with a 2D array, because I'm more familiar with it. Commented Jan 31, 2015 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. Commented Jan 31, 2015 at 22:31

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

The third option is to re-structure the file to provide the meta data required to make decisions about the size of the array
@MadProgrammer: indeed, and I hope you don't mind if I steel this and make it part of the answer.
Not at all, that's why I mentioned it
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.