1

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?
   }
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287

5 Answers5

2

Your code will work, minus the question mark in the last line (edit - this was removed by the editor).

Your braces are also off - move the main function inside the class (you have one too many closing braces after the TestArray constructor).

Edit 2 - now that your question has been edited to fix your errors, it should work as expected. I am not sure if this is the best practice for SO, but that's a discussion for meta.

danben
  • 80,905
  • 18
  • 123
  • 145
1
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);
}

This will work perfectly, although there is a shorter way to initialize anArray:

public static void main(String[] args){
    String[] anArray = new String[] { "Test", "Test2" };
    TestArray work = new TestArray("Jordan", anArray);
}

Which in turn can be shortened to:

public static void main(String[] args){
    TestArray work = new TestArray("Jordan", new String[] { "Test", "Test2" });
}

By the way, String objects are immutable, so you don't have to initialize new strings every time. They can be used directly like this.

Last tip: Arrays.copyOf(anArray, anArray.length) also returns a copy of the array, without the the for-loop.

Marc
  • 3,550
  • 22
  • 28
0

Exactly like you wrote...

Update:

Maybe you got a compiling error, if so, you placed a closing braces (}) too much in your code. I removed it by formatting your code. Maybe this was the problem...

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

Arrays are objects - so are passed them by reference. And you don't need to make copies yourself. If you really feel you have to copy then better use System.arraycopy.

Anton
  • 2,653
  • 1
  • 16
  • 7
0

What do you mean exactly? After you check the matching of {} (main must be into the class), and remove the ? after anArray, the program is compiled and work...: you passed the array correctly to the constructor... if you add

System.out.println(this.array[i]);

you will see your anArray content be printed as expected...

P.S. if your code was not for testing, but you wanted really to copy the array, use System.arraycopy like:

    this.array = new String[anArray.length];
    System.arraycopy(anArray, 0, this.array, 0, anArray.length-1);

See here for example.

Errata corrige it must be anArray.length and not anArray.length-1 since arraycopy wants the length, not the index as I remembered :)

ShinTakezou
  • 9,432
  • 1
  • 29
  • 39