Arrays in Java
Array is a collection of same type of data. It has specified size, can be of one or more dimensions.
Syntax for array declaration
data_type array_name[] = new data_type[size_of_array];
data_type can by any of the primitive data type or user defined data type or any other Java data type, size_of_array specifies number of elements in the array; new is operator used for allocating memory for data, here for array.
int week_day = new int[7]; array of seven elements of type int.
double values = new double [10]; array of 10 elements of type double.
All the elements of array are initialized to zero.
Elements of array are accessed using the index of array and values are assigned to array elements using index of array. Array can be initialized at the declaration of array also.
int aa[] = new int[10];
aa[0] = 33; aa[6] = 32; aa[9] = 43;
int a[] = {1,2,33,44,12,8};
char c[] = new char[6];
c[0] = 'a'; c[3] = 'x'; c[5]= 'w';
In array elements started from index 0 and goes upto the one less than the size of array
class arrays{public static void main(String[] para){int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12};System.out.println("Value at 5th position of array is: "+arr[4]);//here, + concatenate string with integerarr[5] = 44;System.out.println("Value at 6th position after update: "+arr[5]);arr[11] = 67;System.out.println("Value at 12th position after update: "+arr[8]);//arr[12] = 99; here, arr is of size 12 and it stored values from index 0 to 11 onlyint sum = arr[4] + arr[3]; //here + is arithmetic operatorSystem.out.println("Sum: "+sum);System.out.println("Sum: "+(arr[1]+arr[3]+arr[10]+arr[5]));String mountains[] = new String[4];mountains[0] = "Kanchenjunga";mountains[1] = "Annapurna";mountains[2] = "Everest";mountains[3] = "Lhotse";System.out.println("Mountain: "+mountains[0]);System.out.println("Number of Characters: "+mountains[2].length());System.out.println("Character at index 4th position in "+mountains[3]+" is: "+mountains[3].charAt(3));System.out.println("Index of 'r' in "+mountains[2]+" is: "+mountains[2].indexOf('r'));System.out.println(mountains[3]+"--> to Upper Case--> "+mountains[3].toUpperCase());}}Output
Value at 5th position of array is: 5Value at 6th position after update: 44Value at 12th position after update: 9Sum: 9Sum: 61Mountain: KanchenjungaNumber of Characters: 7Character at index 4th position in Lhotse is: tIndex of 'r' in Everest is: 3Lhotse--> to Upper Case--> LHOTSE
Multidimensional Array
Mutlidimensional array is an array of array. Declaring a two-dimesional array.
int twoDimen[][] = new int[3][4];
The above statement allocates a 3 by 4 array and assigns it to twoDimen.
class twodimen{public static void main(String[] abc){int matrix[][] = new int[2][4];matrix[0][0] = 3;matrix[0][1] = 4;matrix[0][2] = 5;matrix[0][3] = 1;matrix[1][0] = 2;matrix[1][1] = 3;matrix[1][2] = 6;matrix[1][3] = 7;System.out.println(matrix[0][0]+"+"+matrix[1][2]+" = "+(matrix[0][0]+matrix[1][2]));System.out.println(matrix[0][3]+"+"+matrix[1][3]+" = "+(matrix[0][3]+matrix[1][3]));}}Output
3+6 = 91+7 = 8
Operators in Java
if statment of Java