5
\$\begingroup\$

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]);
  }
}
\$\endgroup\$
2
  • \$\begingroup\$ are you sure that this is the expected format for printing the array? \$\endgroup\$
    – Ron Klein
    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\$ Oct 12, 2012 at 21:37

3 Answers 3

2
\$\begingroup\$

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 OS
  • StringBuilder 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());
        }
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.