Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Array

Topic/Course
Sub-Topic (Example: name of college)
Array
• Java array is an object which contains fixed set of elements of a similar
data type.
• Array can contains primitives data types as well as objects of a class
depending on the definition of array.
• Index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
• Can get the length of the array using the length member.
Types

• Single dimensional array


• Multi dimensional array
• Jagged/Ragged array
Single dimensional array
Syntax:
//declaration
datatype[] var;
datatype []var;
datatype var[];

//instantiation
var = new datatype[size];

//declaration, instantiation and initialization


datatype var[] = {value1,value2,value3};
1 class Testarray1{
2 public static void main(String args[]){
3 int a[]={33,3,4,5};
4 for(int i=0;i<a.length;i++)
5 System.out.println(a[i]);
6 }
7 }
8
9
10
11
12
13
14
15
Output
33
3
4
5
1 class Testarray2{
2 public static void main(String args[]){
3 int arr[]={33,3,4,5};
4 for(int i:arr)
5 System.out.println(i);
6 }
7 }
8
9
10
11
12
13
14
15
Output
33
3
4
5
Multi dimensional array

• Elements in two-dimensional arrays are commonly referred


by x[i][j] where ‘i’ is the row number and ‘j’ is the column number.
Multi dimensional array
Syntax:
//declaration
datatype[][] var;
datatype [][] var;
datatype var[][];
datatype []var[];

//instantiation
var = new datatype[rowsize][columnsize];

//declaration, instantiation and initialization


datatype var[] = { value1, value2, value3 };
1 class Testarray3{
2 public static void main(String args[]){
3 int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
4 for(int i=0;i<3;i++){
5 for(int j=0;j<3;j++){
6 System.out.print(arr[i][j]+" ");
7 }
8 System.out.println();
9 }
10 }
11 }
12
13
14
15
Output
1 2 3
2 4 5
4 4 5
1 class Testarray4{
2 public static void main(String args[]){
3 int arr[][]={{1,2,3},{4,5,6},{4,4,5}};
4 for(int x[]:arr)
5 {
6 for(int y:x)
7 {
8 System.out.print(y+" ");
9 }
10 System.out.println();
11 }
12 }
13 }
14
15
Output
1 2 3
2 4 5
4 4 5
Jagged/Ragged array

• “Array of arrays” is an array whose elements are arrays.


• 2-D arrays with variable number of columns in each row.
Memory representation
int[][] jaggedArr = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};
Jagged/Ragged array

Example:

int[][] multiDimensionalArr = new int[3][];


multiDimensionalArr[0] = new int[2];
multiDimensionalArr[1] = new int[3];
multiDimensionalArr[2] = new int[4];

int[][] multiDimensionalArr = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};


1 int[][] factors = new int[][] int[] PrimeFactorsOf(int value)
2 { {
3 null, if (value < factors.Length)
4 new int[] { }, {
5 new int[] { 2 }, return factors[value];
6 new int[] { 3 }, }
7 new int[] { 2, 2 }, else
8 new int[] { 5 }, {
9 new int[] { 2, 3}, // . . .
10 new int[] { 7 }, }
11 new int[] { 2, 2, 2 }, }
12 new int[] { 3, 3 },
13 new int[] { 2, 5 },
14 // ...
15 };
Output
1 2 3
2 4 5
4 4 5
• JVM throws an ArrayIndexOutOfBoundsException if length of the array
in negative, equal to the array size or greater than the array size while
traversing the array.
1 public class TestArrayException{
2 public static void main(String args[]){
3 int arr[]={50,60,70,80};
4 for(int i=0;i<=arr.length;i++){
5 System.out.println(arr[i]);
6 }
7 }
8 }
9
10
11
12
13
14
15
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at
TestArrayException.main(TestArrayException.java:5) 50 60 70 80
Question 1
Predict the output

class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}

A) 0 0 B) garbage value garbage value

C) Compiler error D) Exception


Question 2
Predict the output

class Test {
public static void main(String args[]) {
int arr[]= new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}

A) 0 0 B) garbage value garbage value

C) Compiler error D) Exception


Question 3
Predict the output

public static void main (String[] args) {


int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}

A) Same B) Not Same

C) Compiler error D) Runtime error


Question 4
Predict the output
public static void main (String[] args) {
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same");
}

A) Same B) Not Same

C) Compiler error D) Runtime error


Question 5
Predict the output

int a[] = {0,2,4,1,3};


for(int i = 0; i<a.length; i++)
a[i] = a[(a[i]+3)%a.length];

A) 0 B) 1

C) 2 D) 3
Question 6
Predict the output
public static void main(String args[]){
int[] x = {5,6,7,8,9};
int[] y = x;
y[2] = 10;
System.out.println(x[2]);
System.out.println(y[2]);
}

A) 10 10 B) 7 10

C) Compile time error D) None of the above


Question 7
Predict the output

class Sample{
public static void main(String args[]){
int[] x = {5,6,7,8,9};
int y = x;
System.out.println(y);
}
}

A) 5 B) Compile time error

C) Address of x D) None
Question 8
Predict the output

public static void main(String args[])


{
int x[]={1,2,3,4,5},[]y={5,4,3,2,1},i;
for(i=0;i<4;i++)
x[i]=y[i];
System.out.println(x[i]+y[i]);
}

A) 1 B) 6

C) Compile time error D) Run time error


Question 9
What is the value of things[2].length ?

double[][] things ={ {1.2, 9.0},


{9.2, 0.5, 0.0},
{7.3, 7.9, 1.2, 3.9} } ;

A) 2 B) 3

C) 4 D) 9
Question 10
Predict the output
class Test2 {
public static void main(String[] args)
{
String str[] = { “Java", “Programming", “Done" };
System.out.println(str.length);
System.out.println(str[0].length);
}
}

A) Error B) 3 5

C) 3 13 D) None
THANK YOU

You might also like