Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way to do this is with an <code>ArrayList&lt;char[]&gt;</code>. All you have to do is add a new <code>char[]</code> for each new line read:</p> <pre><code>ArrayList&lt;char[]&gt; chars = new ArrayList&lt;&gt;(); while (inputFile.hasNext()){ chars.add(inputFile.nextLine().toCharArray()); } char[][] array = chars.toArray(new char[chars.size()][]); </code></pre> <p>An <code>ArrayList</code> is basically an array of changeable size. This code takes each line in the file, turns it into a <code>char[]</code>, then adds it to the <code>ArrayList</code>. At the end, it converts the <code>ArrayList&lt;char[]&gt;</code> into a <code>char[][]</code>.</p> <p>If you can't or don't want to use <code>ArrayList</code>, you could always do this:</p> <pre><code>char[][] array = new char[1][]; int a = 0; while(inputFile.hasNext()){ //read line and convert to char[]; store it. array[a] = inputFile.nextLine().toCharArray(); //if there are more lines, increment the size of the array. if (inputFile.hasNext()){ //create a clone array of the same length. char[][] clone = new char[array.length][]; //copy elements from the array to the clone. Note that this can be //done by index with a for loop System.arraycopy(array, 0, clone, 0, array.length); //make array a new array with an extra char[] array = new char[array.length + 1][]; //copy elements back. System.arraycopy(clone, 0, array, 0, clone.length); a++; } } </code></pre> <p>If you know the dimensions of the array beforehand:</p> <pre><code>char[][] array = new char[dimension_1][]; int a = 0; while (inputFile.hasNext()){ array[a] = inputFile.nextLine().toCharArray(); a++; //don't need to check if we need to add a new char[] } </code></pre> <hr> <p>In response to comment:</p> <p>We know that a <code>char[][]</code> cannot be printed with <code>Arrays.toString()</code> (if we want the contents) because we will get a lot of <code>char[].toString()</code>. However, a <code>char[][]</code> can be printed with one of the following methods:</p> <pre><code>public static String toString(char[][] array){ String toReturn = "[\n"; for (char[] cArray: array){ for (char c: cArray){ toReturn += c + ","; } toReturn += "\n"; } return toReturn + "]"; } </code></pre> <p>I personally prefer this one (requires <code>import java.util.Arrays</code>):</p> <pre><code>public static String toString(char[][] array){ String toReturn = "[\n"; for (char[] cArray: array){ toReturn += Arrays.toString(cArray) + "\n"; } return toReturn + "]"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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