<p>You can think of a two dimensional array as a grid that is organized by rows then columns.</p>
<p>To declare a two dimensional array do the following</p>
<pre><codeclass="language-java">int[][] grid = new int[5][5]; // Declares a 2D array of 5 rows and 5 columns</code></pre>
<p>You can have as many dimensions as you want. For graphics a 3-dimensional array would render 3D points.</p>
<p>It doesn't have to be inherently visual though, you can have the n-dimensional array look at the interaction between n different variables. For example, the relationships to different questions in a survey.</p>
<p>Strings are essentially a char array with extra methods attached. We can imitate an array of strings with a 2D char array.</p>
<pre><codeclass="language-java">char[][] helloWorld = new char[5][5];
hello[0][0] = 'h';
hello[0][1] = 'e';
hello[0][2] = 'l';
hello[0][3] = 'l';
hello[0][4] = 'o';
hello[1][0] = 'w';
hello[1][1] = 'o';
hello[1][2] = 'r';
hello[1][3] = 'l';
hello[1][4] = 'd';
</code></pre>
<h2>Nested-For Loops</h2>
<p>To access the elements in a 2D array, you need to use a nested for-loop. </p>
<p>Here is how you print out the hello world example above</p>
for (int col = 0; col < helloWorld[row].length; col++) {
System.out.print(helloWorld[row][col]);
}
System.out.print(" ")
}</code></pre>
<p>The code above prints out "hello world"</p>
<h2>2D Arrays in methods</h2>
<p>You can write a get like method in the following way</p>
<pre><codeclass="language-java">public static void get(int[][] array, int row, int col) {
return array[row][col];
}</code></pre>
<p>Arrays in Java are pass by reference not pass by value. Meaning that if you change the array within the method then it will change outside the method.</p>
<pre><codeclass="language-java">public static void insert(int[][] array, int row, int col, int numToInsert) {
array[row][col] = numToInsert;
}</code></pre>
<p>To make it not be able to change the array inside the method, use the keyword <code>const</code> inside the method header. To code below will throw a compiler error.</p>
<pre><codeclass="language-java">public static void insert(const int[][] array, int row, int col, int numToInsert) {
array[row][col] = numToInsert; // This line will throw an errror