Skip to content
Advertisement

Adding an element to the end of a List in java

I have this array that I converted into a list:

JavaScript

How would I go about adding a character to the end of the list since there isn’t a specific add function for Lists?

Edit: Both my classes and code

JavaScript

Advertisement

Answer

  1. you have an array of String and convert it to List of String so you cannot add a char to it.

  2. When you do Arrays.asList you get java.util.Arrays.ArrayList (which is not a java.util.ArrayList). The former has not add function implemented, so you cannot add anything there.

  3. You can construct a “normal” list (java.util.ArrayList) using what you currently have as a constructor parameter.

N.B. you won’t be able to add a char but you can add a String

So that the final solution would be:

JavaScript

Probably you want to achieve something like this:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement