How do I pass an array to a method?

Posted by ambidextorous on Stack Overflow See other posts from Stack Overflow or by ambidextorous
Published on 2010-06-01T15:51:04Z Indexed on 2010/06/01 16:03 UTC
Read the original article Hit count: 190

Filed under:
|

Hey,

I have not been able to find a proper answer on any forums about this.
But how exactly do I pass an array to a class constructor?

public class TestArray {
   String name;
   String[] array;

   public TestArray(String name, String[] anArray){
          this.name = name;
          int len = anArray.length;
          this.array = new String[len];
          for (int i = 0; i < len; i++)
          {
              this.array[i] = new String(anArray[i]);
          }
   }

   public static void main(String[] args){
        String[] anArray = new String[2];
        anArray[0] = new String("Test");
        anArray[1] = new String("Test2");
        TestArray work = new TestArray("Jordan", anArray); // How to pass the array?
   }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays