W
W
WWH2017-10-20 21:48:58
Java
WWH, 2017-10-20 21:48:58

How can I change the values ​​in a two-dimensional array declared in the main function through another function?

How can I change the values ​​in a two-dimensional array declared in the main function through another function? I am writing a sea battle game, I need to change the values ​​in a two-dimensional array through a separate function.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-10-21
@cosmoskey

In Java, arrays are only passed by reference.
Therefore

void change(int[][] field) {
    // Эти команды изменят массив
    field[0][0] = 42;

    field = new int[4][];
    // Теперь изменить оригинальный массив нельзя
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question