11 Array
11 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[];
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
Syntax
for(data_type variable:array)
{
}
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
Syntax
Example:
0 1 2 3
0 10 20 30 40
1 50 60 70 80
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
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
A. Objects
B. Object references
C. Primitive data type
D. None of the above
Question: 02
}
}
Question: 05
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
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
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