Lecture 4
Lecture 4
Arrays
2
Java Arrays
• Normally, an array is a collection of similar type of elements which
has contiguous memory location.
3
Java Arrays
• Arrays are objects which store multiple variables of the same type.
It can hold primitive types as well as object references. In fact most
of the collection types in Java which are the part of java.util
package use arrays internally in their functioning. Since Arrays are
objects, they are created during runtime .The array length is fixed.
4
Types of Array in java
• There are two types of array.
5
Single Dimensional Array in Java
• Syntax to Declare an Array in Java
• First let us get in to declaration of array which holds primitive types.
The declaration of array states the type of the element that the
array holds followed by the identifier and square braces which
indicates the identifier is array type.
7
Creating Arrays
• Example:
• Following statement declares an array variable, myList, creates an array of
10 elements of double type and assigns its reference to myList:
• Following picture represents array myList. Here, myList holds ten double
values and the indices are from 0 to 9.
8
Declaration, Instantiation and
Initialization of Java Array
• We can declare, instantiate and initialize the java array together by:
}
}
• Output:
The size of the array is 4
10
Example of Java Array
public class TestArray {
class ArrayInitializing{
public static void main(String args[]){
int FirstArray[] = new int[6];
for(int i=0;i<FirstArray.length;i++){
FirstArray[i]=i+1;
}
for(int i=0;i<FirstArray.length;i++){
System.out.println(FirstArray[i]);
}
}
}
• The array elements can be accessed with the help of the index.
FirstArray[0] refers to 1 ,FirstArray[1] refers to 2 etc.
12
Java Program to Accept Array
Elements and Calculate Sum
import java.util.Scanner;
Output:
public class Array_Sum
{
public static void main(String[] args)
Enter no. of elements you want in
{
array:5
int n, sum = 0;
Scanner s = new Scanner(System.in);
Enter all the elements:
System.out.print("Enter no. of elements you want in array:");
1
n = s.nextInt();
2
int a[] = new int[n];
3
System.out.println("Enter all the elements:");
4
for(int i = 0; i < n; i++)
5
{
a[i] = s.nextInt();
sum = sum + a[i];
Sum:15
}
System.out.println("Sum:"+sum);
}
}
13
Multidimensional Array in Java
• In such case, data is stored in row and column based index (also
known as matrix form).
14
Multidimensional Array in Java
• Example to initialize Multidimensional Array in Java
– arr[0][0]=1;
– arr[0][1]=2;
– arr[0][2]=3;
– arr[1][0]=4;
– arr[1][1]=5;
– arr[1][2]=6;
– arr[2][0]=7;
– arr[2][1]=8;
– arr[2][2]=9;
15
Example of Multidimensional Java
Array
• Let's see the simple example to declare, instantiate, initialize and
print the 2Dimensional array.
17
Example of Multidimensional Java
Array
public class MatrixMultiplicationExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};