devxlogo

Java Arrays

Java Arrays

C programmers are used to dealing with multidimensional arrays as chunksof contiguous memory which can be accessed through pointer arithmetic.In Java, an array is an actual object, and a multidimensional arraymust be constructed as a an array of arrays. It is an extremely convenientarrangement because you can access the length of an array from withinthe Java language, which allows you to make each array of a differentlength if necessary, without causing undue complications. But thedownside is that you must explicitly instantiate each array, which is notas efficient as allocating one big chunk of memory. To create a 3x3x3array 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 

devx-admin

Share the Post: