intorduction to Arrays in java

6

Click here to load reader

description

introduction to arrays

Transcript of intorduction to Arrays in java

Page 1: intorduction to Arrays in java

Arrays

An array is used to hold the more than one number of homogeneous data.

The length of an array is established when the array is created.

After creation, its length is fixed.

Page 2: intorduction to Arrays in java

Array initialization

The syntax of array initialization is

datatype[] name={item1,item2,...,item n};

OR

name[index position]= item or value;

Page 3: intorduction to Arrays in java

Accessing array elements

Array elements can be either accessed using their index value or using the looping statements

Example

System.out.println(a[1]);

or

for(int i =0 ;some condition; i++)

{ System.out.println(a[i]);}

Page 4: intorduction to Arrays in java

Array resizing

Syntax

aname=Arrays.copyOf(aname, new size);

example

int[] a= new int[4];

a= Arrays.copyOf(a, 8);

Page 5: intorduction to Arrays in java

Types of array

● One dimensional array● Two dimensional array● Multi dimensional array

Page 6: intorduction to Arrays in java

Two dimensional array

The two dimensional arrays are like a table

The Syntax for declaring a 2-D array is

Dtype [][] name= new Dtype[size][size];