2–D Array in Java Program

2-D array is collection of row and column. We can also write row x column. We can also call collection of 1-D array.

 

In advance level, our data will be retrieving from database in one object format, that time we need 2-D array to split data in normal form.

 

In this blog, we see how to store data in 2-D array and how to find maximum number from each row.

Example

C0C1C2Ans
R056342356
R167347373
R245873487

 

C0, C1 and C2 are columns; R0, R1 and R2 are rows.

 

Let’s see how to work in program.

 

import java.util.*;

class arr6

{

public static void main(String args[])

{

Scanner in1 = new Scanner(System.in);

double[][] x = new double[3][3];

int i,j;

 

System.out.println(“Enter Array : “);

for(i=0;i<=2;i++)

{

for(j=0;j<=2;j++)

{

x[i][j] = in1.nextDouble();

}

}

System.out.println(“ Maximum Number From Each Row:“);

double max = 0;

for(i=0;i<=2;i++)

{

max = x[i][0];           // set 1st number of each row

for(j=0;j<=2;j++)

{

if(x[i][j] > max) // compare every row value with max

{

max = x[i][j];  // assign maxvalue into max var.

}

System.out.print(x[i][j] +  “ ”); // display each value

}

System.out.println(“ | ” + max); // display max value

}

}

}

Output:

Enter Array:

23 -12 14     

12 78 22

45 21 43

Maximum Number from Each Row

23 -12 14 | 23

12  78 22 | 78

45  21 43 | 43

 


Basic to Advance Level Tutorial.

 

NoTitle
1Find min and maximum number from 2-D Array
2Find maximum from each column [Hint : use 1-D array for this example]
3Find sum and average of each row
4Count number of positive and negative numbers from each column.

 

 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

206total visits,4visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint