How To Create An Array In Java

How To Create An Array In Java

In java, arrays are li'l bit different then C/C++. But the basic concept is same. Here, i am going to describe Java Arrays with small examples just to understand the concept. If you are a beginner and looking for your head start with arrays then this article might be good start for you as well. Let us now dive right into the concept -

What is an Array?

  • Array is a data structure that can be used to store fixed number of multiple values of similar data type.
  • They are fixed in length. We have to specify the size of an array at the time of the initialization and we can’t change that size at runtime.
  • They are homogeneous. It means we can only assign multiple values of a similar data type to an array. But, we cannot assign multiple values of different data types to an array.
  • They are indexed. It means every block in an array has an index associated with it, starting with 0 to the size of the array -1. It helps to set data to and get data from a particular index.

Array Declarations

There are multiple variations allowed for array declarations. Let us take a look at all of them –

  • Single Dimensional Array Declaration

a) int[ ]   a ;

b) int   a[ ] ;

c) int   [ ]a ;

Note: 

a) First way is recommended because type is clearly separated from the name.

b) At the time of declaration we can’t specify the size.

Example:    int[6]  a ;   This declaration is incorrect

  • Two Dimensional Array Declarations

a) int [ ][ ]     a ;

b) int     [ ][ ] a ;

c) int     a [ ][ ] ;

d) int [ ]    a [ ] ;

e) int [ ]    [ ] a ;

f) int    [ ] a [ ] ;

  • Three Dimensional Array

a) int [ ][ ][ ]     a  ;

b) int      a [ ][ ][ ] ;

c) int      [ ][ ][ ] a ;

d) int [ ]     [ ][ ] a ;

e) int [ ]     a [ ][ ] ;

f) int [ ]    [ ] a [ ] ;

g) int [ ][ ]     a [ ] ;

h) int [ ][ ]     [ ] a ;

i) int     [ ][ ] a [ ] ;

j) int     [ ] a [ ][ ] ;

 

Array Construction

  • Every array in Java is an object. Hence, we can create an array by using new operator.

Example:   int [ ]    a   =   new    int [3] ;

  • We must specify the size of the array at the time of initialization or we will get compile time error.

Example:   int [ ]    a   =  new    int [ ] ;    Compile time error because size not specified

  • An array of size 0 is valid in java.

Example:   int [ ]    a   =  new    int [0] ;

  • An array of –ve size is not allowed in java. If we try to create an array of –ve size then we will get NegativeArraySizeException runtime exception.

Example:   int[ ]    a   =  new   int[-3] ;    Runime Exception: NegativeArraySizeException

  • The allowed datatypes to specify array size are byte, short, int and char.  If we are using any other data type we will get compile time error

Example:

a) byte b = 8 ;                int [ ]  a = new  int [b] ;

b) short s = 25 ;             int [ ]  a = new  int [s] ;

c) int i = 2147 ;              int [ ]  a = new  int [i] ;

d) char c = ‘a’                 int [ ]  a = new  int [c] ;       (array size: 97)

It’s easy to understand example a, b and c as they are all integral data types. But why we can create an array of size of a character? This doesn’t make any sense, right?

But yes! It’s possible to create an array of size of a character.  In this case the compiler will take the ASCII value of the given character as the size of that array because of auto type casting.

Here, the size of the above created array will be 97 because the ASCII value for character ‘a’ is 97.

 

Array Initialization

  • When we create an array automatically every element is initialized with default values.
  • Default value for byte, short, int and long is 0 and for float and double 0.0 and for String is null.
  • We can override these default values with our customized values.
  • If we try to access an array with an index which is out of range of that array then we will get ArrayIndexOutOfBound runtime exception.

 

Array declaration, construction and initialization in a single line

We can declare, construct and initialize an array in a single line.

Declaration:      int [ ]   a ;

Creation:           a = new [3] ;

Initialization:     a[0] = 10;     a[1] = 12;     a[2] = 14;   a[3] = 16;

In Single Line:  int[ ] a = {10, 12, 14, 16}

 

Anonymous Array

  • We can create an array without giving it any name. This types of arrays are called Anonymous Arrays.

Example –       new int [ ] {10,20,30,40}

  • We cannot specify the size of an anonymous array.

Example –      new int [4] {10,20,30,40}         (Incorrect)

  • As we don’t hold anonymous arrays in any variable, these arrays can only be used for one time.

Example –

class Calculator {

        public static void main(String [] args) {

                        sum(new int [] {10,20,30,40});

         }

         private static void sum(int [] values) {

                int total = 0;

                for(int value : values) {

                                total += value;

                }

                System.out.println(“The Sum is ” + total);

          }

}

So, that is how you can declare, create and initialize an array.

ADVERTISEMENT

About  |  Privacy Policy  |  Disclaimer  |  © Copyright 2018. All Rights Reserved.