How do I pass an array to a method?
- by ambidextorous
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?
}
}