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())));
Related
This question already has answers here:
Why does my ArrayList contain N copies of the last item added to the list?
(5 answers)
Closed 3 years ago.
ArrayList<ArrayList<Integer>> aal=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> al= new ArrayList<Integer>();
for(int i=2;i<6;i++)
{
al.add(i);
if(i%2!=0)
{
aal.add(al);
al.clear();
}
}
Output: [[4,5][4,5]]
Required output: [[2,3][4,5]]
al.clear() will also clear the contents of the array list that you have just put into aal. This is because the array list in aal and the one referenced by al are the same array list. Here are two ways you can fix this.
Create a new array list after adding to aal:
for(int i=2;i<6;i++)
{
al.add(i);
if(i%2!=0)
{
aal.add(al);
al = new ArrayList<>();
}
}
Add a copy of al to aal (this is probably what you expected your code would do):
for(int i=2;i<6;i++)
{
al.add(i);
if(i%2!=0)
{
aal.add(new ArrayList<>(al));
al.clear();
}
}
Notice in both of these cases, I am creating new array lists with new ArrayList<>.
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:
Sort List in reverse in order
(9 answers)
Closed 5 years ago.
in my app Android I have a set of data I want to bind to a spinner.
But of these I would like a particular value to be seen as the first in the spinner list.
public String [] getDescriptionCategories () {
Set <String> categories = products.keySet ();
String [] result = new String [categorie.size ()];
int i = 0;
for (String cat: categories) {
result [i ++] = cat;
}
return result;
}
The result is ["Altro","Prodotti","Utenti"], but I wish it was ["Utenti", "Prodotti", "Altro"]
How do I set it up?
You should use return Collections.reverse(result);. It will reverse your list. If you want to sort the list by alphabetical order, see this answer. Hope it helps :)
Use Collection class ie sort(List) and reverse(List)
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));
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);