I’m trying to create an array in one method (or function? Or object? Side question – what’s the difference between all these words?) and then use its length in another method (and I’ll be using it in other places as well). My teacher told me I don’t have to return the array because I’m only modifying the location, so the array isn’t destroyed or something. I would declare it in main but then I can’t size it after I get the size inputs (I don’t think?).
Is anyone following this?
public class Update {
public static void main(String[] args) {
System.out.println("This program will simulate the game of Life.");
createMatrix();
// birthAndLive();
printMatrix();
}
public static void createMatrix() {
Scanner console = new Scanner(System.in);
System.out.println("Please input the size of your board.");
System.out.println("Rows:");
final int rows = console.nextInt();
System.out.println("Columns:");
final int columns = console.nextInt();
System.out.println("Please enter a seed:");
final long seed = console.nextLong();
boolean[][] board = new boolean[rows][columns];
Random seedBool = new Random(seed);
}
public static void printMatrix() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == false)
System.out.print(" - ");
else
System.out.print(" # ");
}
System.out.println();
}
}
You can solve this by passing
boardto your print function.I don’t know exactly how much of the code your teacher allows you to change around though. If all functions need to be called from
main, then you’ll either have to put the array creation code inside the main function, or you’ll have to resort to return statements or class variables.