Java program to demonstrate multidimensional arrays : Array « Collections Data Structure « Java






Java program to demonstrate multidimensional arrays

Java program to demonstrate multidimensional arrays
     
/*
Java Programming for Engineers
Julio Sanchez
Maria P. Canton


ISBN: 0849308100
Publisher: CRC Press
*/


// File name: MultiArray.java
//Reference: Chapter 6
//
//Java program to demonstrate multidimensional arrays
//Topics:
// 1. Simultaneous declaration and initialization
// 2. Use of the length operator to obtain the size
//    of multidimensional arrays

public class MultiArray {
  // Declare constants
  final static int ROWS = 10;

  final static int COLS = 5;

  public static void main(String[] args) {

    // Local varaibles
    int rowCount;
    int colCount;
    int totalSize;

    // Declare and allocate an array of bytes
    byte[][] screenPix = new byte[ROWS][COLS];

    // Obtain and store array dimensions
    rowCount = screenPix.length;
    colCount = screenPix[COLS].length;
    totalSize = rowCount * colCount;

    // To obtain the total number of elements of a
    // two-dimensional ragged array you need to get the size of
    // each array dimension separately

    // Display array dimensions
    System.out.println("Array row size:    " + rowCount);
    System.out.println("Array column size: " + colCount);
    System.out.println("Total size:        " + totalSize);

    //*************************
    //      ragged arrays
    //*************************
    // First allocate the rows of an array
    byte[][] raggedArray = new byte[5][];

    // Now allocate the columns
    raggedArray[0] = new byte[2];
    raggedArray[1] = new byte[2];
    raggedArray[2] = new byte[4];
    raggedArray[3] = new byte[8];
    raggedArray[4] = new byte[3];

    // The resulting ragged array is as follows:
    //  x x
    //  x x
    //  x x x x
    //  x x x x x x x x
    //  x x x

    //************************************
    //     static array initialization
    //************************************
    byte[][] smallArray = { { 10, 11, 12, 13 }, { 20, 21, 22, 23 },
        { 30, 31, 32, 33 }, { 40, 41, 42, 43 }, };

    // Display the array element at row 2, column 3
    System.out.println(smallArray[1][2]); // Value is 21
  }
}


           
         
    
    
    
    
  








Related examples in the same category

1.Initialize a static array
2.Initialization and re-assignment of arraysInitialization and re-assignment of arrays
3.Doubling the size of an arrayDoubling the size of an array
4.Timing array loop performance
5.Array 2DArray 2D
6.Can you change the .length of an array
7.Show Two-Dimensional Array of Objects
8.ArrayListDemo done over using an ArrayList
9.Array Hunt game
10.Multi Dimension Array
11.Clone Array
12.Associates keys with valuesAssociates keys with values
13.Arrays of primitives
14.Creating arrays with new
15.Array initialization
16.Creating an array of nonprimitive objects
17.Create multidimension arraysCreate multidimension arrays
18.Initializing Array ValuesInitializing Array Values
19.Creating a Two-Dimensional Array
20.Initializing a Two Dimensional ArrayInitializing a Two Dimensional Array
21.Using the length VariableUsing the length Variable
22.Triangular array
23.Grow arrayGrow array
24.Define array for class Define array for class
25.String array and output to consoleString array and output to console
26.Multiply two matrices
27.Array Of Arrays Demo 2Array Of Arrays Demo 2
28.Array Copy DemoArray Copy Demo
29.Copying Elements from One Array to Another
30.Extend the size of an array
31.Copy an array
32.Initialize multidimensional array
33.Get array upperbound
34.To get the number of dimensions
35.Resize an array, System.arraycopy()
36.Dump array content: Convert the array to a List and then convert to String
37.java.utils.Arrays provides ways to dump the content of an array.
38.Dump multi-dimensional arrays
39.Use the new shorthand notation to iterate through an array
40.Create a repeated sequence of character
41.Reverse array elements order
42.Convert array of primitives into array of objects
43.Array Initializers
44.Reinitializes a byte array
45.Reinitializes an int array
46.Sum all elements in the array
47.Sums an array of numbers log(x1)...log(xn)
48.A class to iterate over all permutations of an array.
49.Palidrome Array
50.Set of utilities used to manipulate arrays.
51.ArrayUtils provides static methods for manipulating arrays when using a tool such as java.util.ArrayList is inconvenient.
52.Array Util
53.clone two dimensional array