Programming





Arrays in Java

Arrays in Java

by | 30-Mar-2020
Java

Tags:

Share: 


Array is an object which is used to hold a collection of homogeneous elements types. It’s a data structure that is used to store elements in contiguous (next to each other) memory locations.

Properties of Arrays:

  1. While creating the array object, the size must be an integer datatype or a datatype compatible with an integer.
  2. The index of the array starts from 0 (zero) and ends at size of array -1.
  3. For finding the number of elements in an array we can use a method named length.
  4. Array will always be an Object. Even if it stores primitive datatype.

Advantages of Arrays

  1. By using an Array, single variable can be used to manage a large number of elements which will allow us to store, access, search, or sort the elements very easily.
  2. In java array will be always an object.
  3. With respect to memory, Arrays are faster and should be preferred when the number of elements is known at the beginning.

Disadvantages of Arrays

  1. Arrays are fixed in size.
  2. Arrays can contain only homogeneous element types.
  3. The size of an Array must be declared at the time of the creation of the array.
  4. Arrays don't provide readymade method support because there is no underlying data structure

Types of Arrays

  1. Single Dimensional Array: A single dimensional Array can store elements in either row or column-wise.

Syntax

<dataType> <referenceName>[]  = new <datatype>[<size>];

<dataType>[] <referenceName> = new <datatype>[<size>];

<dataType> []<referenceName> = new <datatype>[<size>];

Example

int intArr[] = new int[10];

String[] stringArr = new String[10];

Student []studentArr = new Student[10];

  1. Multidimensional Arrays: Multidimensional arrays are arrays of arrays. Each element of the array is holding the reference to other array.

Syntax

<datatype> <referenceVariableName>[][]  = new <datatype>[<size>][<size>];

<datatype>[][] <referenceVariableName> []  = new <datatype>[<size>][<size>];

<datatype> [][]<referenceVariableName>[]  = new <datatype>[<size>][<size>];

<datatype>[] <referenceVariableName>[][]  = new <datatype>[<size>][<size>];

Example

int intArr[][][]  = new int[10][10];

String[][] stringArr[]  = new String[10][10];

Student [][]studentArr[]  = new Student[10][10];

Employee[] employeeArr[][]  = new Employee[10][10];

  1. Anonymous Array: An Array without a name in called as an Anonymous Array. It is declared and defined at the same time. It is mainly created for instant use like passing it as a parameter or copying it to another array etc.

Syntax

<datatype> <referenceVariableName>[];

<referenceVariableName> = new <datatype>[]{<values> separated with , };

Example

int arr[];

arr = new int[]{23, 2, 10, 12};

Program

public class ArrayExamples {
        public static void main(String args[]) {
               // Single Dimensional Array
              System.out.println("--------Single Dimensional Array----------");
              int singleDimensional[] = new int[5];
 
              singleDimensional[0] = 10;
              singleDimensional[1] = 20;
              singleDimensional[2] = 70;
              singleDimensional[3] = 40;
              singleDimensional[4] = 50;
 
              // Loop Through an Array
              for (int i = 0; i < singleDimensional.length; i++) { // length is the property of array
                     System.out.println(singleDimensional[i]);
              }
 
              // Multi-Dimensional Array
              System.out.println("-------Multidimensional Array----------");
 
              int multiDimensional[][] = new int[2][3];
 
              multiDimensional[0] = new int[] { 1, 2, 4 };
              multiDimensional[1] = new int[] { 5, 6, 69, 66, 58 };
 
              for (int i = 0; i < multiDimensional.length; i++) {
                     for (int j = 0; j < multiDimensional[i].length; j++) {
                           System.out.println(multiDimensional[i][j]);
                     }
               }
 
              // Anonymous Array
              System.out.println("--------------Anonymous Array----------");
 
              // new int[] { 23, 5, 43, 12 } in the next line is anonymous array. We've not assigned it to any variable
 
              for (int i = 0; i < new int[] { 23, 5, 43, 12 }.length; i++) {
                     System.out.println(new int[] { 23, 5, 43, 12 }[i]);
              }
       }
}
 

Output

--------------------Single Dimensional Array--------------------
10
20
70
40
50
--------------------Multidimensional Array--------------------
1
2
4
5
6
69
66
58
--------------------Anonymous Array--------------------
23
5
43
12
";"
Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.