Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Dec 8, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

java (arrays)

How do you create and use a multidimensional array of type int.

C programmers are used to dealing with multidimensional arrays as chunks of contiguous memory which can be accessed through pointer arithmetic. In Java, an array is an actual object, and a multidimensional array must be constructed as a an array of arrays. It is an extremely convenient arrangement because you can access the length of an array from within the Java language, which allows you to make each array of a different length if necessary, without causing undue complications. But the downside is that you must explicitly instantiate each array, which is not as efficient as allocating one big chunk of memory. To create a 3x3x3 array of integers you would do the following:

public final class Matrix {
  public static final void main(String[] args) {
    int i, j;
    int[][] matrix[];

    matrix = new int[3][][];

    for(i = 0; i < matrix.length; i++) {
      matrix[i] = new int[3][];
      for(j = 0; j < matrix[i].length; j++)
	matrix[i][j] = new int[3];
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date