0
String [] Letters = {
    "a", ...... , "z",
};    

new ArrayList<String> (Arrays.asList(Letters));

Am currently using the above code, which I believe it creates an ArrayList from the Array call Letters(Please correct me if I'm wrong). I need to know how to add a new String into the ArrayList. Any help would be greatly appreciated, thank you.

3 Answers 3

2

Use add:

List<String> list = new ArrayList<String> (Arrays.asList(letters));
// Java naming convention - variables start with lower case ^^
list.add("some new string");
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your help. I'm just wondering, wouldn't the system be wasting memories on creating an extra list of array from letters[]? from what I know, new ArrayList<String> (Arrays.asList(Letters)); actually convert letter[] into and arrayList right? or am I wrong on that?
No, it initializes a new arraylist with the content of the array.
if ArrayList<String> (Arrays.asList(Letters)); is initiating a new arraylist what does List<String> list = new ArrayList<String> (Arrays.asList(letters)); do? it seems to me that it's initiating it again.
the same, only assigning the arraylist to a variable, so you can refer to it later, you can use ArrayList<String> list = new ArrayList<String> (Arrays.asList(letters));
1

You could use List.add method

List myList = new ArrayList<String> (Arrays.asList(Letters));
myList.add(myString);

Comments

1

Are you talking about,

String [] Letters = {
    "a", ...... , "z",
};    

ArrayList<String> arrayList = new ArrayList<String> (Arrays.asList(Letters));
arrayList.add("My String");

?

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.