Arrays
Arrays
Java Arrays
JacfuuTechno
3
Features of Array
It is always indexed. Index begins from 0.
It is a collection of similar data types.
It occupies a contiguous memory location.
It allows to access elements randomly.
JacfuuTechno
4
Types of Array in java
There are two types of array.
Single Dimensional Array
Multidimensional Array
JacfuuTechno
5
Single Dimensional Array
A single dimensional array in Java is an array that
holds a sequence of elements, all of the same
type, accessed through an index.
For instance, if we create an array to store integer data
type values, all elements of the array must be integers,
and they are accessed using their index positions.
JacfuuTechno
6
Syntax to Declare an Array in Java
dataType[] arr; (or)
dataType arr[];
Instantiation of an Array in java
arry1 RefVar=new datatype[size];
JacfuuTechno
7
Example of Java Array
class Testarray{
public static void main(String args[]){
int a[]=new int[5];
a[0]=10;
a[1]=20; a[2]=70; a[3]=40; a[4]=50;
for(int i=0;i<a.length;i++)
System.out.println(a[i]); }}
Note:-To find the length of an array, we can use length
property of array object like: array_name.length.
JacfuuTechno
8
Declaration, Instantiation and Initialization of Java Array
We can declare, instantiate and initialize the java
array together by:int a[]={33,3,4,5};
Example:
class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5};
for(int i=0;i<a.length;i++)
System.out.println(a[i]); }}
JacfuuTechno
9
Passing Array to a Method in Java
We can pass the java array to method so that we
can reuse the same logic on any array.
JacfuuTechno
10
Returning Array from the Method
class TestReturnArray{
static int[] get(){
return new int[]{10,30,50,90,60}; }
public static void main(String args[]){
int arr[]=get();
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]); }}
JacfuuTechno
11
ArrayIndexOutOfBoundsException
The Java Virtual Machine (JVM) throws an
ArrayIndexOutOfBoundsException if length of the
array is negative, equal to the array size or greater
than the array size while traversing the array.
public class TestArrayException{
public static void main(String args[]){
int arr[]={50,60,70,80};
for(int i=0;i<=arr.length;i++){
System.out.println(arr[i]); } }}
JacfuuTechno
12
Multi-Dimensional Array
A multi-dimensional array is very much similar to a
single dimensional array.
It can have multiple rows and multiple columns
unlike single dimensional array, which can have only
one row index.
It represent data into tabular form in which data is
stored into row and columns.
JacfuuTechno
13
Multi-Dimensional Array Declaration & Initialization
datatype[ ][ ] arrayName;
Initialization of Array
datatype[ ][ ] arrayName = new
int[no_of_rows][no_of_columns];
JacfuuTechno
14
Example:
class Testarray3{
public static void main(String args[]){
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
JacfuuTechno
15
Any Question?
16
Thank You
17