2

How can i put elements into the following string array?

String[] myString = new String[] {};

Note: Number of element is not fixed.

2
  • 4
    Arrays must be of fixed size. If size is known, you can insert elements this way myString[0] = "somestring" then myString[1] = "someanotherstring" and so on. Use ArrayList if size is not known. Commented Feb 19, 2017 at 2:51
  • Do we need to just write like, String[] myString = new String[] {"element1", "element2".......}; ?? Commented Feb 19, 2017 at 2:56

3 Answers 3

1

You may go through this

How can I add new item to the String array?

and

add string to String array

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

Comments

1
ArrayList.toArray( T[] a )

this would give your array you're trying get.

Comments

1

You cannot.

Since myString is an empty array and since an array's length is fixed, an empty array cannot be changed.

But what you can do is to store a new, different array in the myString variable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.