0

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.

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by tong1
0

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.

0

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 ("");
     } 
  }
}

Edited by tong1: n/a

0

why it is not working like this?

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

(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

Edited by tong1: n/a

1

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 
  }
}

Edited by tong1: n/a

1

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 
  }
}

Edited by tong1: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.