Check my program....i'm printing the values of 2-D array in a ladder form.

int [][] TwoDArray;
 TwoDArray = new int [4][];
   TwoDArray [0] = new int [1];
   TwoDArray [1] = new int [2];
   TwoDArray [2] = new int [3];
   TwoDArray [3] = new int [4];

This is working perfectly fine but when i'm reversing the values of array the program is not working.

TwoDArray = new int [4][];
   TwoDArray [0] = new int [4];
   TwoDArray [1] = new int [3];
   TwoDArray [2] = new int [2];
   TwoDArray [3] = new int [1];

Why this is happening ?

also when i am assigning value like this,

int [][] arrey;
    arrey=new int [3][3] = {{1,2,3},{4,5,6},{7,8,9}};

The program is not working :(

How can i fix these problems.

Recommended Answers

All 7 Replies

the program is not working.

Can you explain what the problems are?
Show the code that does "not work" and explain what you want it to do.

It could be:
int arrey[][]= {{1,2,3},{4,5,6},{7,8,9}};
May I suggest you write a method : void printArray(int a[][]); like the following code by red color to print any 2-D array.
When you create a two-demension ladder form in opposite way, some of the minor changes should be made. The following code works fine:

public class array{
public static void main (String [] args){
 int i, j, k = 100;
 int [][] TwoDArray;
 TwoDArray = new int [4][];
   TwoDArray [0] = new int [4];
   TwoDArray [1] = new int [3];
   TwoDArray [2] = new int [2];
   TwoDArray [3] = new int [1];

  for ( i=0; i<TwoDArray.length; i++ )
     for ( j=0; j<TwoDArray[i].length; j++ ) {     
     TwoDArray [i][j] = k;
     	k = k-10;
     }
   for ( i=0; i<TwoDArray.length; i++ )
     {
     for ( j=0; j<TwoDArray[i].length; j++ )
     System.out.print (TwoDArray [i][j] + " ");
     System.out.println ("");
     } 
  }
}

why it is not working like this?

arrey=new int [3][3] = {{1,2,3},{4,5,6},{7,8,9}};

why it is not working like this?

Please post the full text of any errors you get

(1) Different from C/C++,in Java the following array declaration and allocating space is not allowed:
int arrey [3][3];
(2) the following way to have your arrey is valid:
int arrey[][]; //declaration of a 2-D array
arrey = new int[3][3]; //creates corresponding space for the array arrey with no values assigned.
Then you have to write code to assign values to its elements:

int value=1;
for (int i=0; i<arrey.length; i++)
for (int j=0; j<arrey[i].length;j++)
arrey[i][i]=value++;

(3) Another way to have your arrey: in Java you may declare a array, allocate space, and assign values to it at the same time ( in one line of code), like:
int arrey[][]= {(1,2,3},{4,5,6},{7,8,9}}; //declare, allocate, and assign initial values

I have implemented Xyfuan's latter arrays using two methods: static void init(int a[][]) to assign initial values to each elements and static void printArray(int a[][]) to print a 2D array. it works. Please check if there are any improper remarks/comments.
The output:
100 90 80 70
60 50 40
30 20
10
100
90 80
70 60 50
40 30 20 10

The code:

public class array{
     static void printArray(int a[][]){
     for ( int i=0; i<a.length; i++ ){
       for ( int j=0; j<a[i].length; j++ )
       System.out.print (a[i][j] + " ");
       System.out.println ();
     		} 
     }
     static void init(int a[][]){
     int k=100;
     for (int i=0; i<a.length; i++ )
       for (int j=0; j<a[i].length; j++ ) {     
       a[i][j] = k;
       k = k-10;
     }	
     }

public static void main (String [] args){

 	int [][] TwoDArray1; // declare 2D array TwoDArray1
 	int [][] TwoDArray2; // declare 2D array TwoDArray2
 	TwoDArray1 = new int [4][]; // allocate spaces for 4 rows, i.e. declare 4 1D arrays
	TwoDArray2 = new int [4][]; // allocate spaces for 4 rows, i.e. declare 4 1D arrays
   	for (int i=0; i<4; i++){
  	TwoDArray1[i]= new int[4-i]; // allocate spaces for the 2D array TwoDArray1
   	TwoDArray2[i]= new int[i+1]; // allocate spaces for the 2D array TwoDArray2
   	}
   	
	init(TwoDArray1); // assign initial values to every elements of the 2D array TwoDArray2
	init(TwoDArray2); // assign initial values to every elements of the 2D array TwoDArray1
   	printArray(TwoDArray1); // print all elemens in format of rows and columns 
   	printArray(TwoDArray2); // print all elemens in format of rows and columns 
  }
}

A method to declare and allocate Xufyan's ladder array is written as follows so that the creation of the array seems further concisely in main method.

public class array{
	
	static int[][] allocateLadderArray( boolean up){
		int [][]a; // declare 2D array a
		a = new int[4][]; // allocate spaces for 4 rows, i.e. declare 4 1D arrays
		for (int i=0; i<4; i++) // allocate spaces for the 2D Latter array
		if (up)
		a[i] = new int[4-i];
		else
		a[i] = new int[i+1]; 
		return a;   // return the reference of the allocated array
	}
	static void printArray(int a[][]){
	 for ( int i=0; i<a.length; i++ ){
     for ( int j=0; j<a[i].length; j++ )
     System.out.print (a[i][j] + " ");
     System.out.println ("");
     		} 
     }
     static void assignValues(int a[][]){
     int k=100;
       for (int i=0; i<a.length; i++ )
     for (int j=0; j<a[i].length; j++ ) {     
     a[i][j] = k;
     	k = k-10;
     }	
     }

public static void main (String [] args){

 	int [][] TwoDArray1 = allocateLadderArray(true);  //return a reference of an allocated ladder array to TwoDArray1
 	int [][] TwoDArray2 = allocateLadderArray(false); //return a reference of an allocated ladder array to TwoDArray2
   	
	assignValues(TwoDArray1); // assign initial values to every elements of the 2D array TwoDArray1
	assignValues(TwoDArray2); // assign initial values to every elements of the 2D array TwoDArray2
   	printArray(TwoDArray1); // print all elemens in format of rows and columns 
   	printArray(TwoDArray2); // print all elemens in format of rows and columns 
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.