2

How can i put elements into the following string array?

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

Note: Number of element is not fixed.

Ilham Safeek
  • 127
  • 4
  • 14
  • 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. – Faraz Feb 19 '17 at 02:51
  • Do we need to just write like, String[] myString = new String[] {"element1", "element2".......}; ?? – Ilham Safeek Feb 19 '17 at 02:56

3 Answers3

1
ArrayList.toArray( T[] a )

this would give your array you're trying get.

i.n.n.m
  • 2,678
  • 6
  • 24
  • 48
1

You may go through this

How can I add new item to the String array?

and

add string to String array

Community
  • 1
  • 1
R.A.Munna
  • 1,609
  • 1
  • 13
  • 26
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.

Roland Illig
  • 39,148
  • 10
  • 81
  • 116