The problem presented to me:
Declare a two-dimensional array of ints in main. Write a method to read in the size of each dimension, allocate memory for the array and return it (DON'T DO THIS IN MAIN, BUT CALL THIS METHOD IN MAIN).
Write another method to initialize each element to the sum of its indices (for example, the element at 3, 4 would have the value 7) (DON'T DO THIS IN MAIN, BUT CALL THIS METHOD IN MAIN).
IF TIME, write a method to print the 2-dim. array (use enhanced for loop if you want). (DON'T DO THIS IN MAIN, BUT CALL THIS METHOD IN MAIN)
The output generated by my code:
This is the output which I think is what the above question was asking:
0 1 2 3 4 5 1 2 3 4 5 6 2 3 4 5 6 7 3 4 5 6 7 8 4 5 6 7 8 9
I finished this program to print a 2D array. I got the right output, but I'm not sure if I did what was being asked in the problem below.
Can someone please look at the question below and tell me if I miss any part of the question? The part where I got confused was when it said to read in the size of each dimension:
package twoDarray;
import java.util.Scanner;
public class TwoDarray {
public static void main(String[] args) {
int[][] array= getArray();
init_array(array);
printArray(array);
}
public static int[][] getArray() {
int[][] array = new int[5][6];
return array;
}
public static int[][] init_array(int[][] array) {
for(int i = 0; i< array.length;i++)
for(int j =0; j< array[i].length;j++)
array[i][j]= i+j;
return array;
}
public static void printArray(int[][] array) {
for(int i = 0; i< array.length;i++)
for(int j =0; j< array[i].length;j++)
System.out.printf("%d\n",array[i][j]);
}
}
-
\$\begingroup\$ are you sure that this is the expected format for printing the array? \$\endgroup\$Ron Klein– Ron Klein2012年10月11日 07:53:12 +00:00Commented Oct 11, 2012 at 7:53
-
\$\begingroup\$ Why are you returning the array, when you're obviously using the original reference to the (modified) array? \$\endgroup\$Clockwork-Muse– Clockwork-Muse2012年10月12日 21:37:46 +00:00Commented Oct 12, 2012 at 21:37
3 Answers 3
Coding enhancement
public static int[][] getArray(){
return new int[5][6]; // no need of intermediate object
}
Optimizing speed : never have a computation or an evaluation at each loop, car be very heavy with great number
for(int i = 0, n = array.lenght; i < n ; i++) { ..
as init_array
return array
you can write directly :
printArray(init_array(getArray()));
Use :
System.getProperty("line.separator");
for all OSStringBuilder
then print out :
.
public static void printArray(int[][] array){
String NEWLINE = System.getProperty("line.separator"); // You can put it 'public static' elsewhere
StringBuilder sb = new StringBuilder(); // you can give the size of it between ()
for(int i = 0; i< array.length;i++)
for(int j =0; j< array[i].length;j++)
sb.append(array[i][j])
sb.append(NEWLINE);
}
}
System.out.println(sb.toString());
}
You need to read input (from the command line) using the Scanner class for the dimensions of the array. In this line, you chose 5 and 6:
int[][] array = new int[5][6];
Everything looks good except for that method (getArray
). In that method, you should read in two numbers from the command line, and use those numbers (instead of 5 and 6) as the dimensions of the array.
Naming
Consistent: init_array
might look better if it becomes initArray
Accurate: perhaps processArray
could be used instead of initArray
, since init means some kind of a setup, while the method actually changes the array's values.
In addition, perhaps the class' name could be changed from TwoDarray
to TwoDimArray
, which, at least in the aesthetic aspect (to my own eyes), looks better.