I am trying to create a 2-dimensional array without using ArrayList. I was successful at creating the array but I run into a snag when trying to print the array from another method because the array is not static. However, I cannot make the array static because I am also allowing the user to decided the size of the array. I thought I could override the array and then print it but that only returns null values. Should this be done with a variable-length argument?
public class Array {
public static int[][] array1;
public static void makeArray(){
int [][] array1 = new int [Menu.arrayRow][Menu.arrayCol];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[0].length; j++) {
array1[i][j] = (i * array1[0].length) + j + 1;
}
}
}// end of makeArray
public static void displayArray(){
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[0].length; j++) {
array1[i][j] = (i * array1[0].length) + j + 1;
System.out.print(array1[i][j] + ", ");
}
System.out.println("");
}
}// end of displayArray
This is the result of running the program and choosing item #11 from my menu to display the array:
run:
Enter the number of rows for the array:5
Enter the number of columns for the array:5
========================================
1. Insert a Row
2. Insert a Column
3. Swap rows
4. Swap columns
5. Swap Upper and Lower triangles
6. Display the triangles
7. Reverse the contents of a column
8. Reverse the diagnal
9. Swap diagnals
10. Display a subsection of the current 2D array
11. Display the array
12. Exit
Choose an option from the menu :
11
========================================