Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add elements of a string array to a string array list?

Tags:

java

arrays

I am trying to pass a string array as an argument to the constructor of Wetland class; I don't understand how to add the elements of string array to the string array list.

import java.util.ArrayList;  public class Wetland {     private String name;     private ArrayList<String> species;     public Wetland(String name, String[] speciesArr) {         this.name = name;         for (int i = 0; i < speciesArr.length; i++) {             species.add(speciesArr[i]);         }     } } 
like image 332
robinhood91 Avatar asked Oct 12 '12 06:10

robinhood91


People also ask

How do you add an array of strings to an ArrayList?

For adding an element to the array, First, you can convert array to ArrayList using 'asList ()' method of ArrayList. Add an element to the ArrayList using the 'add' method. Convert the ArrayList back to the array using the 'toArray()' method.

Can you add a string to an ArrayList?

To add an object to the ArrayList, we call the add() method on the ArrayList, passing a pointer to the object we want to store. This code adds pointers to three String objects to the ArrayList... list. add( "Easy" ); // Add three strings to the ArrayList list.


Video Answer


2 Answers

You already have built-in method for that: -

List<String> species = Arrays.asList(speciesArr); 

NOTE: - You should use List<String> species not ArrayList<String> species.

Arrays.asList returns a different ArrayList -> java.util.Arrays.ArrayList which cannot be typecasted to java.util.ArrayList.

Then you would have to use addAll method, which is not so good. So just use List<String>

NOTE: - The list returned by Arrays.asList is a fixed size list. If you want to add something to the list, you would need to create another list, and use addAll to add elements to it. So, then you would better go with the 2nd way as below: -

    String[] arr = new String[1];     arr[0] = "rohit";     List<String> newList = Arrays.asList(arr);      // Will throw `UnsupportedOperationException     // newList.add("jain"); // Can't do this.      ArrayList<String> updatableList = new ArrayList<String>();      updatableList.addAll(newList);       updatableList.add("jain"); // OK this is fine.       System.out.println(newList);       // Prints [rohit]     System.out.println(updatableList); //Prints [rohit, jain] 
like image 167
Rohit Jain Avatar answered Oct 23 '22 05:10

Rohit Jain


I prefer this,

List<String> temp = Arrays.asList(speciesArr); species.addAll(temp); 

The reason is Arrays.asList() method will create a fixed sized List. So if you directly store it into species then you will not be able to add any more element, still its not read-only. You can surely edit your items. So take it into temporary list.

Alternative for this is,

Collections.addAll(species, speciesArr); 

In this case, you can add, edit, remove your items.

like image 38
Ravi A Avatar answered Oct 23 '22 06:10

Ravi A