How Do You Pass an Array to a Method in Java?

Problem scenario
You want to pass an argument to another piece of code (e.g., a function in Java). How do you pass an array as a parameter to another portion of code?

Java only has methods -- not functions (according to this posting).

This code (which should be called will pass a two-dimensional array called twoD to the method "coolp".

import java.util.*;
class contInt
 static String[][] boardprinter(String [][] arr1) {
   return arr1;

 public static void main(String[] args) {
    String twoD[][] = new String[3][3];
    twoD[0][0] = "i";
    twoD[0][1] = "h";
    twoD[0][2] = "g";
    twoD[1][0] = "f";
    twoD[1][1] = "e";
    twoD[1][2] = "d";
    twoD[2][0] = "c";
    twoD[2][1] = "b";
    twoD[2][2] = "a";
    String [][] var1 =  boardprinter(twoD);
    System.out.println("The print function is below from the variable passed to another method.");


 static void coolp(String[][] twoD) {
    String [][] var1 =  boardprinter(twoD);
    String composite1 = "|" + var1[0][0] + "|" + var1[0][1] + "|" + var1[0][2] + "|";

Leave a comment

Your email address will not be published. Required fields are marked *