Arrays in JAVA!!!!
1 — DIMENSIONAL Array
// Initializing array
datatype[] varName= new datatype[size];
// Declaring
datatype[] varName= new type[]{values1, value2,…};
Array with Random Variables
double[] a = new double[n];
for(int i=0; i<n;i++)
{
a[i] = Math.random();
}
Maximum value in an Array
double max = 0;
for(int i=0; i<a.length;i++)
{
if(a[i] > max){
max = a[i]; }
}
Reversing an Array
for(int i=0; i<(a.length())/2;i++){
double temp =arr[i];
arr[i]=arr[n-1-i];
arr[n-1-i]=temp;
}
MULTI — DIMENSIONAL ARRAYS
// Initializing
datatype[][] varName = new datatype[row][col];
// Declaring
datatype[][] varName = {{value1, value2…},{value1, value2…}…};
Transposing a Matrix
for{i = 0; i<row;i++){
for(j = 0; j<column;j++){
System.out.print(array[i][j]+” “); }
System.out.println(“ ”);
}
Multiplying two Matrices
for{int i = 0; i<row;i++){
for(int j=0;j<col2;j++){
for(k=0;k<row2;k++){
sum = sum + first[i][k]*second[k][j];
}
multiply[i][j] = sum;
sum = 0; }
}
Hope this article helped to understand Basic Array programs.
Thanks for reading!!!!