Dacto Dacto - 1 month ago 6
Java Question

pass array to method Java

How can I pass an entire array to a method?

private void PassArray() {
String[] arrayw = new String[4];
//populate array
PrintA(arrayw[]);
}

private void PrintA(String[] a) {
//do whatever with array here
}


How do I do this correctly?

Answer

You do this:

private void PassArray(){
    String[] arrayw = new String[4]; //populate array
    PrintA(arrayw);
}

private void PrintA(String[] a){
    //do whatever with array here
}

Just pass it as other variable. In Java, Arrays are passed by reference.

Comments