0

I'm trying to figure out how to make a constructor that will create a new array with parameter x as the size of the array.

Here is what I mean:

public class ArrayClass{

public String[] elements = new String[];

public ArrayClass(int x) {
    'length of array' = x;
}

I tried to experiment with some of the syntax I know but couldn't figure it out.

Would appreciate some help, thanks in advance!

1 Answer 1

1

Yes. Simply declare your array, and then in the constructor, specify the length:

public String[] elements;

public ArrayClass(int x) {
    elements = new String[x];
}
Sign up to request clarification or add additional context in comments.

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.