Array In Java


Prev Tutorial Next Tutorial

Array in java

Array is a collection of similar type of data. It is fixed in size that means you can't increase the size of array at run time. It is collection of homogeneous data elements. It store the value on the basis of index value.

array in java

Advantage of Array

One variable can store multiple value: The main advantage of array is we can represent multiple value under the same name.

Code Optimization: No, need to declare a lot of variable of same type data, We can retrieve and short data easily.

Random access: We can retrieve any data from array with the help of index value.

Disadvantage of Array

The main limitation of array is Size Limit when once we declare array there is no chance to increase and decrease the size of array according to our requirement, Hence memory point of view array concept is not recommended to use. To over come this limitation in java introduce collection concept.

Types of Array

There are two types of array in java.

  • Single Dimensional Array
  • Multidimensional Array

Array Declaration

Single dimension array declaration.

Syntax

1.   int[]  a;
2.   int  a[];
3.   int  []a;

Note: At the time of array declaration we can not specify the size of array. For Example int[5] a; this is wrong.

2D Array declaration.

Syntax

1.   int[][]  a;
2.   int  a[][];
3.   int  [][]a;
4.   int[]  a[];
5.   int[]  []a;
6.   int   []a[];

Array creation

Every array in a java is an object, Hence we can create array by using new keyword.

Syntax

int[] arr = new int[10];    // The size of array is 10.
or
int[] arr = {10,20,30,40,50};

Accessing array elements

Access the elements of array by using index value of an elements.

Syntax

arrayname[n-1];

Access Array Elements

int[] arr={10,20,30,40};
System.out.println("Element at 4th place"+arr[2]);

Example of Array

public class ArrayEx
{
public static void main(String []args)
{
int arr[] = {10,20,30};
for (int i=0; i < arr.length; i++)
{
 System.out.println(arr[i]);
 }
 }     
 }

Output

10
20
39

Note:

1) At the time of array creation we must be specify the size of array otherwise get an compile time error. For Example
int[] a=new int[]; Invalid.
int[] a=new int[5]; Valid

2) If we specify array size as negative int value, then we will get run-time error, NegativeArraySizeException.

3) To specify array size the allowed data types are byte, short, int, char If we use other data type then we will get an Compile time error.

4) The maximum allowed size of array in java is 2147483647 (It is maximum value of int data type)

Difference Between Length and Length() in Java

length: It is a final variable and only applicable for array. It represent size of array.

Example

int[] a=new int[10];
System.out.println(a.length);  // 10
System.out.println(a.length());  // Compile time error

length(): It is final method applicable only for String objects. It represent number of character present in String.

Example

String s="Java";
System.out.println(s.length());  // 4
System.out.println(s.length);  // Compile time error

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements