Array

• 10-18-2009, 08:29 AM
IT student
Array
hello every one
I am IT student and i face a problem to solve this java program i will be glade if any one can help me question is

Create a 2D array (also called as Matrix) of size NxN.
Display the 2D array.
Create a 2D array of size 2x2 and initialize it with some integer values.
• 10-18-2009, 09:05 AM
pbrockway2
Hi and welcome to Java Forums.

I don't really think anyone is going to write your code for you. It would be no fun for you if they did! But there will always be people able to help if you get stuck and post your code and questions. (why you get a particular compiler error, or why you get output that you don't expect etc).

Have a read of Arrays in Sun's Tutorial - or other parts of the Language Basics section.

Good luck!
• 10-19-2009, 12:47 PM
Gajesh Tripathi
[Solved] 2-D Array
Quote:

Originally Posted by IT student
hello every one
Create a 2D array (also called as Matrix) of size NxN.
Display the 2D array.
Create a 2D array of size 2x2 and initialize it with some integer values.

```/**  * @(#)Array2D.java  *  * @created 19-Oct-2009 4:00 pm  * @author Gajesh  * @version 1.00 2009/10/19  */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Array2D {         public static void main (String[] args) {                 //get value of N                 int N;                 try{                         BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));                         String sForN=buffer.readLine();                         N=Integer.parseInt(sForN);                         //N=Integer.valueOf(sForN).intValue();                 }catch(NumberFormatException e) {                         System.out.println ("you entered invalid entry for N");                         return ;                                }catch(IOException ioe) {                         System.out.println ("An IOException is occured during reading data from consol");                         return ;                 }                 //create a 2-D array                 Object arr[][]=new Object[N][N];                 //initialize the 2-D array                 for (int i = 0; i<N; i++)                         for (int j = 0; j<N; j++)                                 arr[i][j]=i*i;                 //display the 2-D array                 System.out.println ("~~~~ 2-D Array of N*N ~~~~");                 for (int i = 0; i<N; i++) {                         for (int j = 0; j<N; j++)                                 System.out.print (arr[i][j]+" ");                         System.out.println ();                 }                 //             //2-d array of 2*2 and initialize it with integer value             int [][] int2D_array=new int[2][2];             for (int i = 0; i<2; i++)                         for (int j = 0; j<2; j++)                                 int2D_array[i][j]=i*j;     } }```