2

I have the following String list which is constructed as:

String[] messageList = messages.split("(?<=\\G.{" + 9 + "})");

I want to be able to add new strings to the list but of course this would need to be an arraylist.

How could I make an array list from this string list?

Thanks

  • possible duplicate of [Java: How to convert comma separated String to ArrayList](http://stackoverflow.com/questions/7488643/java-how-to-convert-comma-separated-string-to-arraylist) – flup Nov 17 '13 at 14:31

4 Answers4

3

Use Arrays#asList. Try,

List<String> list= new ArrayList<>(Arrays.asList(messageList));
Masudul
  • 21,823
  • 5
  • 43
  • 58
3

You can use Arrays.#asList(T...) to construct a new ArrayList containing the elements in the messageList array.

Before Java 7 :

List<String> l = new ArrayList<String>(Arrays.asList(messageList));

After :

List<String> l = new ArrayList<>(Arrays.asList(messageList));

Or directly :

List<String> l = new ArrayList<>(Arrays.asList(messages.split("(?<=\\G.{" + 9 + "})")));
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
2

Try this:

List<String> lst = new ArrayList<>(Arrays.asList(messageList));

or:

List<String> lst = new ArrayList<>(Arrays.asList(message.split("(?<=\\G.{" + 9 + "})")));

Also check Arrays.asList

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2
List<String> lst = new ArrayList<>(Arrays.asList(messages.split("(?<=\\G.{" + 9 + "})")));
Sandhu Santhakumar
  • 1,678
  • 1
  • 13
  • 24