Monday, January 27, 2020

Arrays

Arrays: A container which stores multiple values of same data type

Declaring an Array:

Traditional Way:

        int a[] = new int[5];
                a[0] = 2;
a[1] = 3;
a[2] = 4;
a[3] = 3;
a[4] = 7;

Most simple way

int b[] = { 12, 3, 4, 5, 6 };

How to retrieve values present in array?

Ans: Using index.

see below program for more and better understanding.

Sample Program 

public static void main(String[] args) {

int a[] = new int[5];
a[0] = 2;
a[1] = 3;
a[2] = 4;
a[3] = 3;
a[4] = 7;// initialized values into that array
int b[] = { 12, 3, 4, 5, 6 };
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);// retrieve values present in the array

}


}


Multidimensional Array

Declaration:
                                  ↓ where 3 is column(see below)
int a[][] = new int[2][3];
                             ↑- where 2 is row(see below)


         C   C    C

R      1    2    3

R      4     5    6

Traditional Way of multidimensional declaration

int a[][] = new int[2][3];

a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;

modern /simple way

int b[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; // multidimensional array


Write a program to get minimum value from above metric.

                      1  2  3

                      4  5  6


There are two rows and three columns.

so i for row & j for column



public class Multidimensional {

public static void main(String[] args) {

int b[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; // multidimensional array

int min = b[0][0];// keeping the first element of matrix as min for our reference

for (int i = 0; i < 2; i++)// row
{
for (int j = 0; j < 3; j++)//column
                          {
if (b[i][j] < min) {
min = b[i][j];
}
}

}
System.out.println(min);
}

}


Note: Execute and understand :)




Now to get max number out off below  matrix.

// 4 5 6
// 5 4 9

public class PrintMaxNumberFromMatrix {

public static void main(String[] args) {
// TODO Auto-generated method stub

// 4 5 6
// 5 4 9
//
int matrix[][] = { { 4, 5, 6 }, { 5, 4, 9 } };
int max = matrix[0][0];

for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] > max)
;

max = matrix[i][j];

}

}
System.out.println(max);
}

}




-------------------


To find the maximum number in the identified colomn(which is lowest in )


public class Check {

public static void main(String[] args) {

int b[][] = { { 6, 4, 3 }, { 4, 0, 6 }, { 3, 2, 7 } }; // multidimensional
// array
int min = b[0][0];// keeping the first element of matrix as min for our
// reference
int minColumn = 0;

for (int i = 0; i < 3; i++)// row
{
for (int j = 0; j < 3; j++)// column
{
if (b[i][j] < min) {
min = b[i][j];
minColumn = j;

}

}

}
int max = b[0][minColumn];
int k = 0;
while (k < 3) {
if (b[k][minColumn] > max) {
max = b[k][minColumn];
}
k++;

}
System.out.println(max);
// System.out.println("the min value in matrix is"+min);

}

}

No comments:

Post a Comment

Selenium_Grid_With_Docker_Compose(yml file)

 docker-compose.yml version: "3" services:   hub:     image: selenium/hub:3.141.59     ports:       - "4444:4444"   chro...