Array in Java

 One Dimensional (1-D) Array:
Array is user defined data type that have fixed size and store similar type data.

Declaration:
Datatype[] arrayName;

e.g.
int[] array;
char[] ch;
String[] str;

Initialization:
Datatype[] arrayName = new Datatype[int size];

It will create  (size *  memory allocated for Datatype) bytes of memory and initialised with default values.

Datatype        Default Values
int                      0
double              0.0
String                null
boolean            false
char                  0x0000 (space)

Anytime you can override the default values with your own values using accessing the array elements using index. Index starts from 0 and ends at size - 1.

e.g. 
int[] array;
array = new int[10];
array[4] = 40;
char[] ch = new char[5];
ch[4]= 'g';
String str[] = new String[20];
str[6] = "Michael";

Ragged 1-D Array:
In ragged array declaration and initialization is only done in one line or one statement. You cannot split the bellow statement in two lines.

e.g.
int[] arr = {1, 2, 3, 4, 5};
String[] str = {"John""Tom""Jerry"};

 Two Dimensional (2-D) Array:
 It is Array of 1-D Array.
 Declaration:
Datatype[][] arrayName;

e.g.
int[][] array;
char[] ch[];
String[] []str;

Initialization:
Datatype[][] arrayName = new Datatype[int rows][int columns];

It will create a matrix(rows * columns *  memory allocated for Datatype) bytes of memory and initialised with default values.

Datatype        Default Values
int                      0
double              0.0
String                null
boolean            false
char                  0x0000 (space)

Anytime you can override the default values with your own values using accessing the array elements using rows index and column index. Rows index starts from 0 and ends index at rows - 1 and Column index starts from 0 and ends index at column - 1

e.g. 
int[][] array;
array = new int[10][5];
array[4][2] = 40;
char[] ch = new char[5][3];
ch[4][0] = 'g';
String str[] = new String[20][4];
str[6][3] = "Michael";

Ragged 2-D Array:
In ragged array declaration and initialization is only done in one line or one statement. You cannot split the bellow statement in two lines.

e.g.
int[][] arr = {{1, 2, 3, 4, 5},{10,20,30}};
String[][] str = {{"John""Tom""Jerry"}, {"abcd", "pqrs"}, {"books", "author", "title"}};

Example 1:
package com.java.arrays;

public class Array {

    public static void main(String[] args) {
        /*System.out.println("Hello World");
        int[] array = new int[15];
        System.out.println(array.length);
        for (int i = 0; i < array.length; i++) {
            array[i] =i+100;
        }
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i] );
        }*/

        
        int[] arr = {1, 2, 3, 4, 5}; 
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        String[] str = {"John""Tom""Jerry"};
        for (int i = 0; i < str.length; i++) {
            System.out.print(str[i] + " ");
        }
        /*System.out.println(str[0]);
        System.out.println(str[1]);
        System.out.println(str[2]);*/

        System.out.println();
        
        int[][] matrix = new int[3][2];
        
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = i + j;
            }
        }
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
        
        double[][] darr = new double[2][];
        darr[0] = new double[3];
        darr[1] = new double[6];
        System.out.println(darr.length);
        System.out.println(darr[0].length);
        for (int i = 0; i < darr.length; i++) {
            for (int j = 0; j < darr[i].length; j++) {
                darr[i][j] = i + j;
            }
        }
        
        for (int i = 0; i < darr.length; i++) {
            for (int j = 0; j < darr[i].length; j++) {
                System.out.print(darr[i][j] + " ");
            }
            System.out.println();
        }
        
        int[][] a = {{1,2,34,567,89,9,98,8},{10, 20,56,87}};
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }    }
}

Output:
1 2 3 4 5
John Tom Jerry
0 1
1 2
2 3
2
3
0.0 1.0 2.0
1.0 2.0 3.0 4.0 5.0 6.0
1 2 34 567 89 9 98 8
10 20 56 87

No comments:

Post a Comment