Arrays are collection of elements of a similar type.
An array is a data structure in Java which stores a sequential collection of elements of a fixed size and having the same data type. Only a fixed set of elements can be stored here.
Arrays are objects in java.
Arrays is a class in java package: java.util.Arrays We can define arrays in two ways:
int x =10;
int a[] ={ 10, 20, 30, 40 };
double []d = {1.2, 2.3, 3.4 };
int a[] = new int[4];
OR
int a[] = null;
a = new int[4];
a[0] = 11;
a[1] = 22;
a[2] = 201 + 1 * 4; // = 201+4 =205
a[3] = 201 + 2 * 4; // = 209
Example 1:
package com.javabykiran.basics;
public class Lab26 {
public static void main(String[] args) {
// int b[5]=null;
// int d[]=new int[];
int d2[] = null;
System.out.println(d2);
d2 = new int[0];
System.out.println(d2); //array is there, but empty
// d2[0]=123; //error in java.lang ArrayIndexOutOfBounds
//Exception: 0
//d2 = new int[-1]; //error java.lang.NegativeArraySizeException
d2 = new int[9];
int a[] = null;
System.out.println(a);
// a ={10,20,30}; // error (must be at initializer)
int x[] = {10, 20, 30};
System.out.println(x);
int n = x.length;
for (int i = 0; i < n; i++) {
System.out.println(x[i]);
}
System.out.println(a);
a = new int[5];
System.out.println(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i] + " ");
}
System.out.println(" ");
a[0] = 11;
a[2] = 222;
a[4] = 444;
// a[7]=777;
for (int i = 0; i < a.length; i++) {
System.out.println(a[i] + " ");
}
}
}
Output:
null
[I@15db9742
null
[I@6d06d69c
10
20
30
null
[I@7852e922
0
0
0
0
0
11
0
222
0
444
Example 2:
package com.javabykiran.Arrays;
public class Lab27 {
public static void main(String[] args) { int a1[] = new int[5];
int n = 5;
int a2[] = new int[n];
// int a3[]=new int[5.5]; //error possible
// loss of precision found:
// double required:
int byte b = 5;
int a4[] = new int[b];
int a5[] = new int[(2 + 3) * 2];
// int a6[]=new int[(2+3.5)*2];
// found:double required:int
}
}
Boolean a2[]=new Boolean[5];
a2[0]=true;
a2[1]=true;
String x[]=new String[5];
x[0]="java";
x[2]="by";
x[4]="kiran";
It is also called as arrays of array.
int a[][]=new int[3] [4];
(Number of arrays required) Three arrays required
int x[][]={{1,2,3},{4,5},{6,7,8,9},{1,2} }
int s[][]=new int[4][]; //OK
int xx[][]=new int[][]; //error size in first bracket is must
int s[][] = new int[4][];
s[0] = new int[3];
s[0][0] = 1;
s[0][1] = 2;
s[0][2] = 3;
s[1] = new int[3];
s[1][0] = 1;
s[1][1] = 2;
s[1][2] = 3;
s[2] = new int[3];
s[2][0] = 1;
s[2][1] = 2;
s[2][2] = 3;
s[3] = new int[3];
s[3][0] = 1;
s[3][1] = 2;
s[3][2] = 3;
/* s[4] = new int[3]; //will not correct at runtime
s[4][0] = 1;
s[4][1] = 2;
s[4][2] = 3;*/
package com.javabykiran.Arrays;
import java.util.Arrays;
public class Lab30 {
public static void main(String[] args) {
int a[] = { 4, 7, 2, 1, 3, 8, 6, 10, 9 };
System.out.println("Before sorting");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i] + " ");
}
// System.out.println(a[i]+" ");
// System.out.println(" ");
Arrays.sort(a); // merge sorting
System.out.println("after sorting");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i] + " ");// 1 2 -----10
}
System.out.println("");
System.out.println(Arrays.binarySearch(a, 7));
System.out.println(Arrays.binarySearch(a, 12));
System.out.println(" ");
int x[][] = {{3,1,2},{5,6,7},{9,8,7,9},{2,4,3}};
System.out.println("Before sorting");
for (int i = 0; i < x.length-1; i++) {
for (int j = 0; j < x.length-1; j++) {
System.out.println(x[i][j] + " ");
}
System.out.println(" ");
}
for (int i = 0; i < x.length; i++) {
Arrays.sort(x[i]);
}
System.out.println("After sorting");
for (int i = 0; i < x.length; i++) {
for (int j = 0; j < x[i].length; j++) {
System.out.println(x[i][j] + " ");
}
System.out.println(" ");
}
int s5[] = new int[6];
s5[0] = 10;
s5[4] = 44;
for (int i = 0; i < s5.length; i++) {
System.out.println(s5[i] + " ");
}
Arrays.fill(s5, 99);
for (int i = 0;i < s5.length; i++) {
System.out.println(s5[i] + " ");
}
}
}
package com.javabykiran.Arrays;
public class Lab13 {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
if ((a > b) && (a > c)) {
System.out.println("max" + a);
} else if (b > c) {
System.out.println("max" + b);
} else {
System.out.println("max" + c);
}
}
}
package com.javabykiran.Arrays;
public class Lab14 {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
if (n % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd Number");
}
}
}
package com.javabykiran.Arrays;
public class Lab15 {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
switch (a) {
case 1:
System.out.println("1jun10");
break;
case 2:
System.out.println("19may87");
break;
default:
System.out.println("The number is out of range");
}