This question already has answers here:
How to add elements of a string array to a string array list?
(10 answers)
Closed 6 years ago.
I have 2 String Arrays
String[] question1 = {"Q1","Q2","Q3","Q4"};
String[] question2 = {"Q5","Q6","Q7","Q8"};
And one ArrayList
ArrayList<String> aList = new ArrayList<String>();
How can I manipulate them if i want to access to a member of ArrayList. I tried converting both arrays to String but it doesn't give solution.
ArrayList<String> aList = new ArrayList<String>(Arrays.asList(question1));
aList.addAll(Arrays.asList(question2));
Related
This question already has answers here:
How to store all ArrayList<ArrayList<String>> values into ArrayList<String>?
(2 answers)
Closed 4 years ago.
I have a source list of type ArrayList<ArrayList<String>>. I want to put all the strings from the source list into a destination list of type ArrayList<String>. How do I do this?
Example:
sourceList = { {"a","b"}, {"c","b"} }
destinationList = { "a","b","c","d" }
You can use for-each and addAll():
for (ArrayList<String> subList : listOfLists) {
listofStrings.addAll(subList);
}
See:
https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
https://docs.oracle.com/javase/7/docs/api/java/util/List.html#addAll(java.util.Collection)
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 5 years ago.
I have created an ArrayList from an Array. I want to print one random item from this ArrayList.
How do I do this? I have inserted the code.
public class Song {
final String [] songArray = {
"song1",
"song2"
};
ArrayList<String> songList = new ArrayList<>(Arrays.asList(songArray));
}
You can try this.
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
Random random = new Random();
System.out.println(list.get(random.nextInt(list.size())));
This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 8 years ago.
How do I replace the following array with an ArrayList.
Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
You can do something like
List<Employee> companyTeam = Arrays.asList(manager, engineer1, superviso1, accountant, intern);
Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
List<Employee> list=Arrays.asList(companyTeam);// array to List
You already have an array, So you can use Arrays.asList(companyTeam) to convert array to List
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 9 years ago.
I want to know how I can name a variable dynamically.
If there are 3 items in a list then it should create 3 different object for each item.
Like:
ArrayList<String> list1;
ArrayList<String> list2;
ArrayList<String> list3;
So it should count up or anything like that.
if there are 4 items in a list it should create 4 variables.
How can I achieve that?
Edit:
I tried this but it says me an error that I can't create a generic array of Info.
ArrayList<Info> listForLg[] = new ArrayList<Info>[];
for (int i = 0; i < sizeOfKfzList; i++) {
listForLg[i] = new ArrayList<Info>();
listForLg[i].add(logInfo);
}
Can you help me?
Use arrays
ArrayList<String> list[] = new ArrayList<String>[3]; //or 4 or n
Then access them like
list[0] = "123";
Use Array for this...
ArrayList<String> yourlist[] = new ArrayList<String>([your size here])
Based on index use your variable..
yourlist[0] yourlist[1]....and so on
Hope this could help
Create list or arraylist
ArrayList<ArrayList<String>> group = new ArrayList<ArrayList<String>>(4);
or
List<List<String>> group1 = new ArrayList<List<String>>(4);
Edit:
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
To add (u can add items dynamically by loop)
group.add(list1);
and to get
list2= group.get(0);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert String[] to ArrayList<String>
hi please can anyone help me I have :
private String results[];
private ArrayList<String> alist;
I want convert
String results[] to ArrayList<String>
Convert String Array to ArrayList as
String[] results = new String[] {"Java", "Android", "Hello"};
ArrayList<String> strlist =
new ArrayList<String>(Arrays.asList(results));
You can use the Arrays.asList() method to convert an array to a list.
E.g. List<String> alist = Arrays.asList(results);
Please note that Arrays.asList() returns a List instance, not an ArrayList instance. If you really need an ArrayList instance you can use to the ArrayList constuctor an pass the List instance to it.
Try this:
ArrayList<String> aList = new ArrayList<String>();
for(String s : results){
aList.add(s);
}
What this does is, it constructs an ArrayList of Strings called aList: ArrayList<String> aList = new ArrayList<String>();
And then, for every String in results: String s : results
It add's that string: aList.add(s);.
Hope this helps!
You should use
Arrays.asList(results)
by default, unless you absolutely for some reason must have an ArrayList.
For example, if you want to modify the list, in which case you use
new ArrayList(Arrays.asList(results))