0% found this document useful (0 votes)
69 views

11 Array

Here are some practice programs using arrays in Java: 1. Write a Java program to reverse an array: ```java import java.util.Arrays; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int temp; for(int i=0; i<array.length/2; i++) { temp = array[i]; array[i] = array[array.length-i-1]; array[array.length-i-1] = temp; } System.out.println(Arrays.toString(array)); } } ```

Uploaded by

Manoj Manu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

11 Array

Here are some practice programs using arrays in Java: 1. Write a Java program to reverse an array: ```java import java.util.Arrays; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int temp; for(int i=0; i<array.length/2; i++) { temp = array[i]; array[i] = array[array.length-i-1]; array[array.length-i-1] = temp; } System.out.println(Arrays.toString(array)); } } ```

Uploaded by

Manoj Manu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Array

What you’ll Learn

● Single Dimensional Array


● Multi Dimensional Array
● Jagged Array
Array

● An object which contains elements of a similar data type.


● Array in Java is index-based.
● First Element of Array is stored 0 index.
Pros and cons of Array

Advantages

● Code Optimization
● Random access

Disadvantages

● Size Limit
● Can store a single type of primitives only.
1D Array

0 1 2 3 4
10 20 30 40 50

Array Length 5
Single dimension Array
Syntax:
dataType[] arr;
(or)
dataType []arr;
(or)
dataType arr[];

Instantiation of an Array in Java:


arrayRefVar = new datatype[size];
Example

class Main{
public static void main(String args[]){
int arr[] = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 70;
arr[3] = 40;
arr[4] = 50;
for(int i = 0;i < a.length;i++)
System.out.println(a[i]);
}
}
Example

class Main{
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]);
}
}
For Each Loop for Array

● Prints the array elements one by one.


● It holds an array element in a variable, then executes the body of the loop.

Syntax

for(data_type variable:array)
{

//body of the loop

}
Example

class Main{
public static void main(String args[]){
int[] scores = new int[10];
scores = new int[3];
scores = new int[] {215, 234, 218, 189, 221, 290};
for(int score : scores)
{
System.out.print(score + " ");
}
}
Passing Array to Method
class Main{
static void arrmethod(int arr[]){
for(int i = 0;i < arr.length;i++)
System.out.println(arr[i]);
}
public static void main(String args[]){
int a[] = {33,3,4,5};
arrmethod(a);
}
}
Return Array From Method

class Subclass class Main{


{ public static void main(String args[]){
int[] arrmethod(int[] arr) int a[] = {1,22,33,44};
{ Subclass S1 = new Subclass();
return arr; int[] return_arr = S1.arrmethod(a);
} for(int i = 0; i < return_arr.length; i++){
} System.out.println(return_arr[i]);
}
}
}
Anonymous Array
public class Main{
static void AnonymousArr(int arr[]){
for(int i = 0;i < arr.length;i++)
System.out.println(arr[i]);
}
public static void main(String args[]){
AnonymousArr(new int[]{10,20,40,60});
}
}
Predict the Output

public class Main{


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]);
}
}
}
Multidimensional Array

● Data is stored in row and column based index.


● Also known as matrix form.

Syntax

dataType[][] RefVar; (or) dataType [][]RefVar;


(or)
dataType RefVar[][]; (or) dataType []RefVar[];

Example:

int[][] arr = new int[3][3];


Multi Dimensional Array

0 1 2 3

0 10 20 30 40

1 50 60 70 80

2 90 100 110 120


Example
class Main{
public static void main(String args[]){
int arr[][] = {{1,2,3},{2,4,5},{4,4,5}};
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Jagged Array
class Main{
public static void main(String[] args){
int arr[][] = new int[3][];
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[2];
int count = 0;
for (int i = 0; i < arr.length; i++)
for(int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Class Name of Array

An array is an object.For array object, a proxy class is created whose name


can be obtained by getClass() .getName() method on the object.
class Main{
public static void main(String args[]){
int arr[] = {1,2,3,4};
Class c = arr.getClass();
String name = c.getName();
System.out.println(name);
}
}
Copying Array

We can copy an array to another by the arraycopy() method of System class.

Syntax

public static void arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
Predict the output

class Main{
public static void main(String[] args){
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(String.valueOf(copyTo));
}
}
Cloning an Array
● Java array implements the Cloneable interface.
● Clone of a single-dimensional array,it creates the deep copy of the Java array.It
means, it will copy the actual value
● Clone of a multidimensional array, it creates the shallow copy of the Java array
which means it copies the references.
Example
class Main{
public static void main(String args[]){
int arr[] = {1,2,3,4};
System.out.println("Printing original array:");
for(int i:arr)
System.out.println(i);
System.out.println("Printing clone of the array:");
int carr[] = arr.clone();
for(int i:carr)
System.out.println(i);
System.out.print("Are both equal? : ");
System.out.println(arr == carr);

}
}
Adding 2D Array
class Main{
public static void main(String args[]){
int a[][] = {{1,3,4},{3,4,5}};
int b[][] = {{1,3,4},{3,4,5}};
int c[][] = new int[2][3];
for(int i = 0;i < 2;i++){
for(int j = 0;j < 3;j++){
c[i][j] = a[i][j] + b[i][j];
System.out.print(c[i][j] +" ");
}
System.out.println();
}
}
}
Multiply 2D Array

public class Main{


public static void main(String args[]){
int a[][] = {{1,1,1},{2,2,2},{3,3,3}};
int b[][] = {{1,1,1},{2,2,2},{3,3,3}};
int c[][] = new int[3][3];
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
c[i][j] = 0;
for(int k = 0;k < 3;k++)
c[i][j] += a[i][k] * b[k][j];
System.out.print(c[i][j] +" ");
}
System.out.println();
}
}
}
Predict the Output

class Main
{
public static void main(String args[])
{
String[] computer = {"RAM","HDD","MOUSE"};
String[] parts = {computer[0],computer[2]};
System.out.print(parts[1]);
}
}
Predict the Output

class Main{
public static void main(String args[]){
int arr[] = new int[10];
for (int i = 0; i < 10; ++i)
{
arr[i] = i;
System.out.print(arr[i] + " ");
i++;
}
}
}
Predict the Output

class Main
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}
Predict the Output
class Main{
public static void main(String args[]){
int a[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + a[i][j];
System.out.print(sum / 5);
}
}
Predict the Output

class Main{
public static void main(String args[]){
String[] ary = {"KITE", "AIR"};
String str = "PLANE";
ary[1] = str;
str = "FLY";
System.out.println(ary[1]);
}
}
Practice programs

1. Write a Java program to reverse an array of integer values.


2. Write a Java program to find the maximum and minimum value of an array.
3. Write a Java program to add two matrices of the same size.
4. Write a Java Program to Display Upper Triangular Matrix.
Question: 01

In Java arrays are

A. Objects
B. Object references
C. Primitive data type
D. None of the above
Question: 02

How do you initialize an array in Java?

A. int arr[3] = (1,2,3);


B. int arr(3) = {1,2,3};
C. int arr[3] = {1,2,3};
D. int arr(3) = (1,2,3);
Question: 03

Which one of the following is a not valid


statement?
A. char[] c = new char[5];
B. char c[] = new char[5];
C. char []c = new char[5];
D. char c[5] = new char[5];
Question: 04

//Predict the output:


import java.util.Scanner;
public class EthnusArray
{
A. 30
public static void main(String args[])
{ B. 40
int arr[] = {10, 20, 30, 40, 50}; C. No Output
System.out.print(arr[3]);
D. Compiler Error

}
}
Question: 05

//Predict the output:


class EthnusArray {
public static void main(String args[])
{ A. Garbage Values
int arr[]= new int[3]; B. 00
System.out.print(arr[0]+“ ”);
C. No Output
System.out.print(arr[1]+“ ”);
} D. Compilation error
}
Question: 06

//Predict the output:


public class EthnusArray
{
public static void main(String[] args) A. 10 100 50
{
int i = 50; B. 10 10 10
int[] a = new int[10]; C. Compilation Error
System.out.print(a.length + “ ”);
a = new int[100]; D. 10 100 100
System.out.print(a.length + “ ”);
a = new int[i];
System.out.print(a.length + “ ”);
}
}
Question: 07
//Predict the output:
public class EthnusArray
{
public static void main(String s[]) A. 25 0 50
{ B. 25 null 50
int a[] = new int[5]; C. 25 0 Exception
a[2] = 25; D. No Output
System.out.print(a[2]); E. ArrayIndexOutofBoundException
System.out.print( a[3]);
a[5] = 50;
System.out.print(a[5]);
}
}
Question: 08

public class EthnusArray


{
public static void main(String[] args)
{
A. No Output
int a[] = new int[0];
System.out.print(a.length); B. 0
} C. Compiler Error(method is length())
} D. Compiler Error(Size can’t be zero)
Question: 09

public class EthnusArray


{
public static void main(String[] args)
{ A. Compile time error
int a[] = new int[0];
System.out.print(a[0]); B. ArrayIndexOutOfBoundException
} C. 0
}
D. No Output
Question: 10

//Predict the output:


public class EthnusArray
{
public static void main(String[] args) A. Compiler Error
{
int[] x = {120, 200, 016 }; B. 120 200 016
for(int i = 0; i < x.length; i++) C. 120 200 16
System.out.print(x[i] + " ");
} D. 120 200 14
}
Question: 11
class EthnusArray
{
public static void main(String args[])
{
A. Sum =21
int a[] = new int[50];
int i = 27 % 11; B. Sum = 20
int j = 5 * 3;
int k = i - j; C. Sum =40
a[i] = i;
a[j] = j;
D. Compiler Error
int sum = 0;
for(int l = 0; l < a.length; l++)
{
sum += a[l];
}
System.out.println("Sum = " + sum);
} }
Question: 12
class EthnusArray
{
public static void main(String[] args)
{
int a[] = {2};
a = new int[]{5}; A. x=4, x=3, x=2, x=1
int x = a[0];
while (x >= 0) B. x=3, x=2, x=1, x=0, x=-1
{
process(a); C. x=1, x=0, x=-1, x=-2, x=-3
x = a[0];
x--; D. Compiler Error
System.out.print("x=" + x + "," );
}
static void process(int[] a)
{
int x = a[0]--;
x++;
}
}
Question: 13
public class EthnusArray {
public static void main(String[] args)
{
int[] array = {12,15,11,13,9,25};
for(int i = 0; i < array.length-1; i++)
A. 12 15 11 13 9 25
{ B. 9 11 12 13 15 25
if (array[i] > array[i + 1])
{ C. 12 11 13 9 15 25
int temp = array[i];
array[i] = array[i + 1]; D. ArrayIndexOutOfBound
array[i + 1] = temp;
} Exception
}
for(int i=0;i<array.length;i++)
{
System.out.print(array[i] + " ");
}
} }
Question: 14

public class EthnusArray


{
public static void main(String s[])
A. 3, 3
{
int s_array[] = new int[39]; B. 3, 0
s_array= new int[]{10, 20, 30};
C. Compile Error(can’t assign
int[] f_array = null;
f_array = s_array; null to an array)
System.out.print( f_array.length + ", " );
D. NullPointerException
System.out.println(s_array.length);
}
}
Question: 15

public class EthnusArray


{
public static void main(String[] args)
{ A. \”Prints =0 5 2 \”
int[] a = new int[]{0,1,2,3,4,5};
System.out.print("\"Prints = "); B. Compile time error
System.out.print(a[0]+ a[5]+ a[2]+ "\""); C. “Prints = 7”
}
} D. \”Prints = 7\”
Question: 16

class EthnusArray
{
public static void main(String[] args) A. Same
{
int arr1[] = {1, 2, 3}; B. Not Same
int arr2[] = {1, 2, 3};
C. No output
if (arr1 == arr2)
D. Error
System.out.println("Same");
else
System.out.println("Not same");
}
}
Question: 17

class EthnusArray
{
public static void main(String[] args) A. Same
{
int arr1[] = {1, 2, 3}; B. Not Same
int arr2[] = {1, 2, 3};
C. No output
if(Arrays.equals(arr1, arr2))
D. Error
System.out.println("Same");
else
System.out.println("Not same");
}
}
Question: 18

public class EthnusArray


{
public static void main(String[] args)
{ A. 3 2 15
int []a=new int[]{5,1,15,20,25};
int i,j,m; B. 2 3 20
i=++a[1]; C. 2 1 15
j=a[1]++;
m=a[i++]; D. 125
System.out.print(i +" " );
System.out.println(j +" ");
System.out.println(m +" ");
}
}
Question: 19
public class EthnusArray
{
public static void main(String[] args)
{
int count = 0; A. 4
int[] numbers = {-5,4,-5,3,-2,-4}; B. 2
for (int j = 0; j < numbers.length; j++) C. 6
{ D. Compiler Error
if(numbers[j] < 0 && numbers[j] % 2 != 0)
{
count++;
}
}
System.out.println(count);}
}
Question: 20
What is the value of arr[2].length ?

int[][] arr ={ {10,20},


{30,40,50,60},
{70,80,90,100,110,120} } ; A. 2
B. 4
C. 6
D. 3
Question: 21

class EthnusArray
{
public static void main(String s[])
{
int[][]input={{3,5,6,7},{2,4},{1},{2,3,4,5}}; A. Result = 604800
int result = 1; B. Result = 12
for(int i = 0; i < input.length; i++)
{ C. Result = 15
for(int j = 0; j < input[0].length; j++) D. Compilation Errors
{ E. Exception
result *= input[i][j];
}
}
System.out.println("Result = " + result);
}
}
Question: 22
class EthnusArray
{
public static void main(String s[])
{
int[][] input={{3,5,6,7},{2,4},{1},{2,3, 4,5}};
A. Result =12;
int result = 1; B. Result = 604800
int k = 1; C. Only 2 and 3 are true.
for(int i = 0; i < input.length; i++) D. Throw
{
for(int j = 0; j < input[k].length; j++) ArrayIndexOutofBoundExce
{ ption
result *= input[i][j];
}
k++;
}
System.out.println("Result = " + result);
} }
Question: 23

public class EthnusArray


{
public static void main(String[] args)
{
int[] a1d = {}; A. 020010221
int[] b1d = {1, 3}; B. 020000221
int[][] a2d = {}; C. Some other output
int[][] b2d = {{}}; D. 02
int[][] c2d = {{1, 2}, {5}}; ArrayIndexOutOfBoundsExc
System.out.print(a1d.length + " " + b1d.length eption
+ " ");
System.out.print(a2d.length +" "+ a2d[0].length
+ " " + b2d.length + " " + b2d[0].length + " ");
System.out.print(c2d.length + ""+ c2d[0].length + "
" + c2d[1].length);
}
}
Question: 24

public class EthnusArray


{
public static void main(String args[])
{
int arr[] = new int[] {0,1,2,3,4,5,6,7,8,9}; A. 0
int n = 6; B. Compiler error
n = arr[arr[n] / 2]; C. 1
System.out.println(arr[n] / 2); D. 3
}
}
Question: 25
public class EthnusArray
{
public static void main(String args[])
{
int[] input = {3, 5, 6, 7}; A. 0
int output = mul(input); B. 630
System.out.print(output); C. 330
}
D. 1
public static int mul(int[] input)
{
int args = 1;
for(int i = 0; i <= input.length - 1; i++)
{
args *= input[i];
}
return args; }
}
Question: 26
public class EthnusArray{
public static void main(String args[])
{ A. 42 5 19
int array[][]={{42,2,23},{5,60,12},{19,10,1}}; 2 60 10
function(array); 23 12 1
}
public static void function(int array[][]) B. 42 2 23
{ 5 60 12
for (int i = 0; i < 3; i++) 19 10 1
{
for (int j = 0; j < 3; j++)
{
System.out.print(array[j][i] + " ");
}
System.out.println();
} } }
Question: 27

public class EthnusArray{


public static void main(String args[])
{ A. No output
int array[][] = { { 1, 2, 3, 4},
B. 16
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}, C. Exception
{13, 14, 15, 16}
D. Error
};
System.out.print(array[4][4] + " ");
}
}
Question: 28
class EthnusArray
{ A. 2D Jagged Array
public static void main(String[] args)
{ 00
int arr[][] = new int[2][];
arr[0] = new int[3]; 00
arr[1] = new int[2];
int count = 0; B. 2D Jagged Array
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
012
arr[i][j] = count++;
System.out.println("2D Jagged Array");
for (int i = 0; i < arr.length; i++) {
3 4
for (int j = 0; j < arr[i].length; j++)
System.out.print(arr[i][j] + " "); C. Exception
System.out.println();
} D. Error
}
}
Question: 29

class EthnusArray
{
public static void main(String[] args)
{
int a[] = { 1, 8, 3 }; A. Content of a[] 1 8 3
int b[] = new int[a.length];
b = a; Content of a[] 2 8 3
b[0]++;
System.out.print("Contents of a[] " + " "); B. Content of a[] 2 8 3
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " "); Content of a[] 2 8 3
System.out.print("\nContents of b[] " + " ");
for (int i = 0; i < b.length; i++) C. Exception
System.out.print(b[i] + " ");
} D. Error
}
Question: 30

class EthnusArray
{
public static void main(String[] args)
A. Content of a[] 1 8 3
{
int a[] = { 1, 8, 3 }; Content of a[] 2 8 3
int b[] = new int[a.length];
b = a.clone(); B. Content of a[] 2 8 3
b[0]++;
System.out.print("Contents of a[] " + " "); Content of a[] 2 8 3
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " "); C. Exception
System.out.print("\nContents of b[] " + " ");
for (int i = 0; i < b.length; i++) D. Error
System.out.print(b[i] + " ");
}
}
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

[email protected] +91 7815 095 095 +91 9019 921 340

You might also like