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?
   }
}
2
  • @Joachim Sauer: Haha, we were editing this question at the same time... Commented Jun 1, 2010 at 16:01
  • 1
    You have to accept an answer. Commented Jun 2, 2010 at 13:53

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow this was exactly the problem. I had one to many braces and changing that one bracket made everything work. THANks!
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.

1 Comment

Thanks for the tip for copying the array.
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.

3 Comments

'so are passed them by reference'. close, but not true: stackoverflow.com/questions/40480/is-java-pass-by-reference
your reasoning is otherwise correct, but technically a reference is passed by value
@seanizer: agree - java passes references by value :)
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 :)

Comments

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.