Skip to content

Instantly share code, notes, and snippets.

@tgruber5150
Created September 14, 2012 20:17
Show Gist options
  • Save tgruber5150/3724462 to your computer and use it in GitHub Desktop.
Save tgruber5150/3724462 to your computer and use it in GitHub Desktop.
This exercise demonstrates the nature of 3 dimensional arrays. It might be helpful to think of them as an indexed box, rather than 3 lists.
import java.util.Random;
import java.util.Scanner;
public class MultiDimArray {
static Random rand = new Random();
static int count = 0;
public static void main(String[] args) {
// 3-D array with fixed length:
System.out.println("This program demonstrates the breadth of 3-dimensional arrays.");
System.out.println("You pick each array size, and the program will assign a number");
System.out.println("to each point in the 3 dimensional array.");
System.out.println("Lets get started:");
System.out.println("How many items do you want in the first array?");
int a = enterANumber();
System.out.println("How many in the 2nd array?");
int b = enterANumber();
System.out.println("How many in the 3rd?");
int c = enterANumber();
int[][][] point = new int[a][b][c];
for(int i = 0; i < point.length; i++)
for(int j = 0; j < point[i].length; j++)
for(int k = 0; k < point[i][j].length; k++) {
++count;
point[i][j][k] = count;
}
for(int i = 0; i < point.length; i++)
for(int j = 0; j < point[i].length; j++)
for(int k = 0; k < point[i][j].length; k++)
System.out.println("a2[" + (i+1) + "][" + (j+1) + "][" +
(k+1) + "] = " + point[i][j][k]);
}
public static int enterANumber() {
Scanner in = new Scanner (System.in);
return Integer.parseInt(in.next());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment