Alice in Action with Java Chapter 12 Arrays and Lists in Java.
intorduction to Arrays in java
Click here to load reader
-
Upload
muthukumaran-subramanian -
Category
Education
-
view
56 -
download
0
description
Transcript of 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.
Array initialization
The syntax of array initialization is
datatype[] name={item1,item2,...,item n};
OR
name[index position]= item or value;
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]);}
Array resizing
Syntax
aname=Arrays.copyOf(aname, new size);
example
int[] a= new int[4];
a= Arrays.copyOf(a, 8);
Types of array
● One dimensional array● Two dimensional array● Multi dimensional array
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];