Note that there are some explanatory texts on larger screens.

plurals
  1. POReading a .txt file into a 2d arraylist
    text
    copied!<p>Im creating a game in android, where the levels are going to be stored in separate .txt files. Each level will be a grid of characters that will represent a different item on the map, but each level will be a varying size, so I want to create a robust piece of code that will read a file, and store each level in a 2d arraylist, no matter what its size is.</p> <p>My first attempt:</p> <pre><code>private void loadLevel(String filename) { mapWidth = 0; mapHeight = 0; BufferedReader br = null; try { String line = null; br = new BufferedReader(new InputStreamReader(mContext.getAssets().open(filename))); while ((line = br.readLine()) != null) { mapArray.add(getLevelLine(line)); mapHeight++; } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException e1) { e1.printStackTrace(); } } } private ArrayList&lt;Character&gt; getLevelLine(String line) { ArrayList&lt;Character&gt; levelLine = new ArrayList&lt;Character&gt;(); if (line == null) return levelLine; char[] lineArray = line.toCharArray(); for (char mapPiece : lineArray) { levelLine.add(mapPiece); } mapWidth = lineArray.length; return levelLine; } </code></pre> <p>This is a little inefficient, because the mapWidth is recalculated on each line, and it doesnt work, because the first horizontal line of the text file is read, and stored in the first vertical column on the arraylist, so it copies the text file, but the x and y coordinates are swapped.</p> <p>Attempt 2:</p> <pre><code> private void loadLevel(String filename) { mapWidth = 0; mapHeight = 0; BufferedReader br = null; try { String line = null; br = new BufferedReader(new InputStreamReader(mContext.getAssets().open(filename))); while ((line = br.readLine()) != null) { mapArray.add(new ArrayList&lt;Character&gt;()); char lineArray[] = line.toCharArray(); for (char mapPiece : lineArray) { mapArray.get(mapHeight).add(mapPiece); } mapHeight++; mapWidth = lineArray.length; } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException e1) { e1.printStackTrace(); } } } </code></pre> <p>This calculates the mapWidth in the same way, so still seems a little inefficient. The hope was that by adding an empty entry into the arraylist, I could loop through the ith element of each of them. This second attempt also doesnt increase the mapHeight correctly, because on the last itteration, the mapHeight will be increased, and then the while loop will not be executed again, but for some reason even if I subtract 1 from mapHeight after the while loop, I get an index out of bounds error. More importantly, by manually setting the mapWidth and mapHeight, my second attempt still seems to swap the x and y coordinates when storing it into the arraylist.</p> <p>Is there anything obvious I am missing? It seems there should be a relatively simple way of doing this, without requiring a pre-read of the text file, and avoiding using a normal char array.</p> <p>Thanks in advance for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload