Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

ARRAYS

An array is an indexed collection of fixed number of homogenous data elements.

Advantages:

 Represent huge number of values using single variable so that readability of the code will be
improved

Limitations:

 Arrays are fixed in size. Once we create an array, we cant increase or decrease size based on our
requirement. Hence to use arrays we should be aware of the size in advance, which may not be
possible always

Array Declaration

1D array declaration

Int[] x;

Int []x;

Int x[];

All 3 are valid declarations. But we are recommended to use the first one as it seperates the variable
from the data type
 At the time of declaration, we cant specify the size otherwise we’ll get compile time error

o Int[6] x is invalid
o Int[] x is valid

2d array declaration

All the declaration shown above are valid.

All the declarations other than the last one are valid.

If we are specifying the variable after the dimension[], its applicable only for the first variable. If we are
trying to apply for remaining variables we will get compile time error
3D array declaration

Array creation

Every array in java is a object. Hence we can create arrays using new operator

Int[] a = new int[3]

We can create objects only for classes. For every array type, corresponding classes are available and
these classes are part of java language and not available to the programmer level
 At the time of creation, we should specify the size else we will get compiler error


 Int [] x = new int[0] is valid. Array of size 0 is valid.
 If we are trying to specify array size with negative int value then we will get runtime exception
saying NegativeArraySize Exception eg: int[] x = new int[-6]
 To specify array size, the allowed data types are int, char, byte and short

 The max size of int array is 2147483647 which is the max value of int value type
2D Array creation

In Java, two dimensional arrays is not implemented by using matrix style. SUN people followed array of
arrays approach for multidimensional array creation. The main advantage of this approach is memory
utilization will be improved.

Eg1:

Int [] [] x = new int [2] []

X[0] = new int[2]

X[1] = new int [3]

Eg2: 3D array
Array Initialization:

Once we create an array, every element is initialized with default values

Note:

Whenever we are trying to print any reference variable, internally toString() method will be called which
is implemented by default to return the string in the following format: classname @ hashcode in
hexadecimal form eg: [I @3e2345

Eg:

Every array will be initialized with default values, if we are not satisfied with default values then we can
override these values with our values eg:x[0] =10, x[2] =30

For array with size 5


Either positive or negative value then we will get runtime exception saying
arrayindexoutofboundexception

Array Declaration, creation and initialization in one line

We can declare create and initialize an array in a single line ( shortcut representation)

Int[] x = {10,20,30}

Char[] c ={‘a’,’e’,’i’}

String[]s = {“a”,”basdf”, “adsfes”}

For multidimensional array: we can use this shortcut for multidimensional array also

Int[] = {{10, 20}, {30,40,50}}

If we want to use this shortcut, we should perform all activities in a single line. If we are trying to split it
into multiple lines then we will get compile time error.
Length vs Length()

Length

Length is a final variable applicable for arrays. Length variable represents the size of array.

Int[] x= new int[6];

s.o.p(x.length()) -> compile time error

s.o.p(x.length) -> 6

Length():

Length() method is a final method applicable for string objects,

Length() method returns no. of characters present in the string


String s =”durga”

Sop(s.length) -> Compile time error

Sop(s.length()) -> 5

Note: Length variable is applicable for arrays and not applicable for string objects and length() method
is applicable for string objects but not for arrays

Example: String array

In Multi-dimensional array length variable represents only the base size not the total size

Int[][] x= new int [6][3]

Sopln (x.length) -> 6

Sopln(x[0].length) -> 3

There is no direct way to find total length of multi dimensional array but indirectly we can find as
follows:

X[0].length + x[1].length+…

Anonymous Arrays
We can declare arrays without name, such type of arrays are called as anonymous array.

The main purpose of anonymous arrays is just for instant use.

We can create anonymous array as follows: new int[] {10,20,30,40}

While creating anonymous array if we specify the size, we’ll get CE if we provide size Eg: new int[3]
{10,20,30}

Array element Assignments: Case1 for primitive array

Case2: for Object array

In the case of object type arrays as array elements we can provide either declared type objects or its
child class objects
Case3:

We can create interface type arrays and it can hold objects of its implementing class

Tabular form:
Array variable Assignments

Case1:

Element level promotions are not applicable at primitive array level

Ex: char element can be promoted int type but char array cannot be promoted to int array

For Object arrays, child class arrays can be used to point to parent class array

Ex:

String [] s= {“a”, “adf”}


Object o =s; -> valid

Case2:

Whenever we are assigning one array to another array, internal elements will not get copied, only the
reference variables will be reassigned.

Case3:

Whenever we are assigning one array to another array, the dimensions must be matched.

Ex:

In the place of 1D int array we should provide 1D array only. If we are trying to provide any other
dimension then we’ll get compile time error.
Note: when we copy one array to another array types and dimensions should match but sizes need not
to be matched

Ex: Playing with arrays

You might also like