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

I am trying to pass a string array as an argument to the constructor of the Wetland class; I do not understand how to add elements of a string array to a list of string arrays.

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]); } } } 
+42
java arrays
Oct 12
source share
11 answers

You already have a built-in method: -

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

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

Arrays.asList returns another ArrayList java.util.Arrays.ArrayList , which cannot be pointed to java.util.ArrayList .

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

NOTE : - The list returned by Arrays.asList is a list of fixed sizes. If you want to add something to the list, you will need to create another list and use addAll to add elements to it. So then you better go with the second method as shown 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] 
+80
Oct 12
source share

I prefer it

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

The reason is that the Arrays.asList () method will create a fixed-size list. Therefore, if you directly store it in a view, then you cannot add any element, but it is not readable. You can, of course, edit your subjects. Therefore, translate it into a temporary list.

The alternative for this is

 Collections.addAll(species, speciesArr); 

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

+12
12 Oct. '12 at 7:11
source share

You must create an instance of ArrayList before trying to add elements:

 private List<String> species = new ArrayList<String>(); 
+7
12 Oct.
source share

I think I will add this to the mix:

 Collections.addAll(result, preprocessor.preprocess(lines)); 

This is a change that Intelli recommends.

from javadocs:

 Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of <tt>c.addAll(Arrays.asList(elements))</tt>, but this method is likely to run significantly faster under most implementations. When elements are specified individually, this method provides a convenient way to add a few elements to an existing collection: <pre> Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon"); </pre> 
+7
Jul 11 '15 at 23:27
source share

Arrays.asList is the bridge between the array and the collection array, and it returns a list of fixed sizes supported by the array.

 species = Arrays.asList(speciesArr); 
+5
Oct 12
source share

In Java 8, the syntax for this is greatly simplified and can be used to perform this conversion in a compressed form.

Note that you will need to change your field from a specific implementation to the List interface so that this works smoothly.

 public class Wetland { private String name; private List<String> species; public Wetland(String name, String[] speciesArr) { this.name = name; species = Arrays.stream(speciesArr) .collect(Collectors.toList()); } } 
+2
Apr 11 '16 at 22:52
source share

Arrays.asList is a handy feature available in Java to convert an array variable to a list or assembly. For a better understanding, consider the example below:

 package com.stackoverflow.works; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Wetland { private String name; private List<String> species = new ArrayList<String>(); public Wetland(String name, String[] speciesArr) { this.name = name; this.species = Arrays.asList(speciesArr); } public void display() { System.out.println("Name: " + name); System.out.println("Elements in the List"); System.out.println("********************"); for (String string : species) { System.out.println(string); } } /* * @Description: Method to test your code */ public static void main(String[] args) { String name = "Colors"; String speciesArr[] = new String [] {"red", "blue", "green"}; Wetland wetland = new Wetland(name, speciesArr); wetland.display(); } } 

Output:

enter image description here

+1
Oct. 12
source share
 ArrayList<String> arraylist= new ArrayList<String>(); arraylist.addAll( Arrays.asList("mp3 radio", "presvlake", "dizalica", "sijelice", "brisaci farova", "neonke", "ratkape", "kuka", "trokut")); 
+1
Jun 28 '14 at 20:26
source share

Use the asList() method. From java Doc asList

 List<String> species = Arrays.asList(speciesArr); 
0
Oct. 12
source share

Arrays.asList() method simply returns a List type

 char [] arr = { 'c','a','t'}; ArrayList<Character> chars = new ArrayList<Character>(); 

To add an array to a list, first convert it to a list, and then call addAll

 List arrList = Arrays.asList(arr); chars.addAll(arrList); 

The next line will cause a compiler error

 chars.addAll(Arrays.asList(arr)); 
0
May 15 '17 at 15:51
source share
 public class duplicateArrayList { ArrayList al = new ArrayList(); public duplicateArrayList(Object[] obj) { for (int i = 0; i < obj.length; i++) { al.add(obj[i]); } Iterator iter = al.iterator(); while(iter.hasNext()){ System.out.print(" "+iter.next()); } } public static void main(String[] args) { String[] str = {"A","B","C","D"}; duplicateArrayList dd = new duplicateArrayList(str); } } 
-one
Jul 19 '16 at 14:24
source share



All Articles