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]);
}
}
}
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);
}
}
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.
ArrayList<String> arraylist= new ArrayList<String>();
arraylist.addAll( Arrays.asList("mp3 radio", "presvlake", "dizalica", "sijelice", "brisaci farova", "neonke", "ratkape", "kuka", "trokut"));
Thought I'll add this one to the mix:
Collections.addAll(result, preprocessor.preprocess(lines));
This is the change that Intelli recommends.
from the javadocs:
Adds all of the specified elements to the specified collection._x000D_
Elements to be added may be specified individually or as an array._x000D_
The behavior of this convenience method is identical to that of_x000D_
<tt>c.addAll(Arrays.asList(elements))</tt>, but this method is likely_x000D_
to run significantly faster under most implementations._x000D_
_x000D_
When elements are specified individually, this method provides a_x000D_
convenient way to add a few elements to an existing collection:_x000D_
<pre>_x000D_
Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");_x000D_
</pre>
_x000D_
You should instantiate your ArrayList
before trying to add items:
private List<String> species = new ArrayList<String>();
Arrays.asList
is the handy function available in Java to convert an array variable to List or Collection. For better under standing consider the below example:
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:
Arrays.asList()
method simply returns List
type
char [] arr = { 'c','a','t'};
ArrayList<Character> chars = new ArrayList<Character>();
To add the array into the list, first convert it to list and then call addAll
List arrList = Arrays.asList(arr);
chars.addAll(arrList);
The following line will cause compiler error
chars.addAll(Arrays.asList(arr));
Arrays.asList is bridge between Array and collection framework and it returns a fixed size List backed by Array.
species = Arrays.asList(speciesArr);
In Java 8, the syntax for this simplifies greatly and can be used to accomplish this transformation succinctly.
Do note, you will need to change your field from a concrete implementation to the List
interface for this to work 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());
}
}
Use asList()
method. From java Doc asList
List<String> species = Arrays.asList(speciesArr);
Source: Stackoverflow.com