I need to create 50 arraylists but rather than having to initialise them all individually. Is there a way in JAVA that I can do this dynamically??
i.e.
pseudo code:
for int i=1; i<51; i++
List<String> Counti = new ArrayList<String>();
So that in the loop it goes through and create 50 arraylists all called Count1, Count2, Count3 etc up until Count50.
I've tried creating a string and then naming the list by the string but it doesnt seem to recognise that teh name is a variable.
e.g.
for int i=1; i<51; i++
String Name="Count "+i
List<String> Name = new ArrayList<String>();
Instead it just creates a list called "Name"
You can do this with reflection, but this is a pretty bad idea. What you probably want to do is create an arraylist of arraylists.
ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();
for (int i = 0; i < 50; i++)
listOfLists.add(new ArrayList<String>());
You should store them in a List and not create 50 variables
List<List<String>> lists = new ArrayList<ArrayList<String>>();
for (int i = 0; i < 50; i++)
{
lists.add(new ArrayList<String>());
}
You need a List of ArrayList.
List<List<String>> lists = new ArrayList<>();
for (int i = 0; i < 50; i++){
lists.add(new ArrayList<String>());
}
one another possible solution is to use a HashMap like this to name the ArrayLists:
Map<String, ArrayList<String>> mAllArrayListsByName = new HashMap<String, ArrayList<String>>();
for (int i = 0 ; i < 50 ; i++){
mAllArrayListsByName.put("List"+String.valueOf(i), new ArrayList<String>());
}
ArrayList<String> lMyArrayList = mAllArrayListsByName.get("List0"); // get List 0
Related
I have project to create storage, and i am using ArrayLists for every room.
Is there any solution to create more than one ArrayList in loop ?
I just want to reduce amount of code.
Quotes/brackets and other stuff arent working. Is there any solution for beginner?
I was trying something like this.
for(int i=0; i<10; i++}{
ArrayLists list[i] = new ArrayLists();
}
ArrayList list0 = new ArrayList();
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
//up to 10
use arrays.fill and get rid off the loop
ArrayList<Integer>[] al = new ArrayList[5];
Arrays.fill(al, new ArrayList<Integer>());
I am not entirely sure what you want to achieve, but you can have a list of lists like so:
ArrayList<ArrayList<>> listOfLists = new ArrayList<>();
for (int i = 0; i < 10; i++) {
listOfLists.add(new ArrayList<String>());
}
List<List<Integer>> lists = new ArrayList<List<Integer>>();
for (int i = 0; i < 5; i++) {
List<Integer> list = new ArrayList<>();
lists.add(list);
}
Will create Multiple List inside a List.
I am receiving my data from the database in the type of
Map<String,ArrayList<String>
So, the columns' names are the keys, and the columns values are stored in the
ArrayList<String>
I need to convert these hash to Object [][] data; for presenting data.
I know, how simple solutions like loops work.
But i would like to know what is the shortest solution to convert
ArrayList<String> to Object[][] data
I am using java 8.
Let's say, at the beginning i have
Map<String, ArrayList<String>> res
Point:
I have the data in ArrayList<String> means that, I have access to data vertically, but i in Object [][] it is somehow horizontally.
As i said res is the database table, so Object [][] would be a row, meanwhile the ArrayList<String> is the column
How about this?
Object[][] result = res.entrySet()
.stream()
.map(e -> e.getValue().toArray())
.collect(Collectors.toList())
.toArray(new Object[0][0]);
The resulting array has the dimension: result[col][row]. So when iterating:
for(int col = 0; col < result.length; col++) {
for(int row = 0; y < result[col].length; row++){
result[col][row]; //table cell
}
}
Java 8 provides an efficient solution for converting it to a data object as stated well by #Gerald in the above answer.
However, if you are also interested in doing this in the naive approach then I have implemented the code as follows.
Map<String, ArrayList<String>> map = new HashMap<>();
String[] arr1 = { "I", "am", "good" };
List<String> list = Arrays.asList(arr1);
map.put("Max", new ArrayList<String>(list));
arr1 = new String[]{"I", "am", "always", "excited"};
list = Arrays.asList(arr1);
map.put("Peter", new ArrayList<String>(list));
System.out.println(map);
// OUTPUT: {Max=[I, am, good], Peter=[I, am, always, excited]}
Object[][] data = new Object[map.size()][];
int i = 0;
// iterate the map and store to Object data
for(Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
data[i] = new Object[entry.getValue().size() + 1]; // number of records from list + (One) map key
data[i][0] = entry.getKey(); // save key at '0'th index
int j = 1; // iterate the ArrayList to save values in the same row.
for(String s : entry.getValue()) {
data[i][j++] = s;
}
i++;
}
// Printing the data object
for(i = 0; i < data.length; i++) {
for(int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println();
}
// OUTPUT :
//Max I am good
//Peter I am always excited
I use a variable class which stores an integer. If the integer is 13, I need 13 arraylists to be created. Any idea as to how I would do this?
This is what is in my while loop:
ArrayList<Integer[]> list + count = new ArrayList<>();
With the following:
ArrayList<ArrayList<Integer>> myLists = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < 4; i++) {
ArrayList<Integer> newList = new ArrayList<Integer>();
myLists.add(newList);
}
Let's say I need the integer 9 to go into the 3rd array list and the integer 3 to go to the 2nd array list. How would i do that?
What are you trying to achieve?
You can create an ArrayList of ArrayLists. So something like:
ArrayList<ArrayList<Integer>> myLists = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < numOfLists; i++) {
ArrayList<Integer> newList = new ArrayList<Integer>();
myLists.add(newList);
}
That way you'll have myLists with all the lists you created.
By concatinating the variable to make another variable name is not possible. The compiler should already told you about this. You can accomplish this by using another ArrayList
List<List<Integer[]>> mainList = new ArrayList<List<Integer[]>>();
for(int i = 0; i < 13; i++){
mainList.add(new ArrayList<Integer[]>());
}
If you want to identify each of the ArrayList by a name you could use a Map.
Map<String, ArrayList<Integer[]>> listsMap = new HashMap<>();
for(int i = 0; i < 13; i++){
listsMap.add("list" + i, new ArrayList<>());
}
Then, if you need to add an integer to a particular list you could do this:
listsMap.get("list3").add(new Integer[]{3});
I created an ArrayList of 80 Arraylists. Each ArrayList inside, has the values 1-9.
However, when I delete a value from one of the Arraylists its deleted in all of them.
ArrayList<List> available = new ArrayList<List>();
ArrayList<Integer> possibleValues = new ArrayList<Integer>();
for(int j = 1; j<=9; j++){
possibleValues.add(j);
}
for (int i = 0; i<=80; i++){
available.add(possibleValues);
}
int b = (int) available.get(0).get(2);
available.get(0).remove(0);
String s = available.get(80).toString();
System.out.println(" " + s);
}
Any help is appreciated.
The problem here is that you've added the same ArrayList possibleValues to available 81 times. So, the available list contains 81 references to the same possibleValues list.
Any changes made to the list are visible through any of those 81 references.
If you don't want the changes visible to all references, just one, then you need to make 81 copies of the list to add:
for (int i = 0; i<=80; i++){
available.add(new ArrayList<Integer>(possibleValues));
}
That's because you're adding the same ArrayList to every index of the outer ArrayList . Any change to this ArrayList will automatically result in a change in the other ArrayList .
Nest your loops to always create a new ArrayList or construct a new ArrayList from the already existing one.
Because you are adding the values to same ArrayList. You need to create new Arraylist for 0 to 80 iterations.
for(int j = 1; j<=9; j++){
possibleValues.add(j);// one ArrayList with values.
}
for (int i = 0; i<=80; i++){
available.add(possibleValues); //Here adding same Arraylist 80 times.
}
Try this one
ArrayList<List> available = new ArrayList<List>();
ArrayList<Integer> possibleValues = new ArrayList<Integer>();
for (int i = 0; i<=80; i++){
possibleValues = new ArrayList<Integer>();
for(int j = 1; j<=9; j++){
possibleValues.add(j);
}
available.add(possibleValues);
}
You are adding the same ArrayList 81 times. ArrayList is a reference type, so any change to it will affect all items. It should be:
available.add(new ArrayList(possibleValues));
im having trouble making an array of arraylists, heres's the code : 'ArrayList<Integer>[] solucao= new ArrayList[6];'
and using this code below:
solucao[0].add(1);
solucao[0].size();
solucao[1].size();
solucao[1].add(1);
solucao[2].size();
solucao[2].add(1);
solucao[3].size();
solucao[3].add(1);
solucao[4].size();
solucao[4].add(1);
solucao[5].size();
solucao[5].add(1);
solucao[6].size();
solucao[6].add(1);
solucao[7].size();
solucao[7].add(1);
all the calls for size return null. Anyone knows how to solve it?
Im looking for a data structure of array of arraylists, as each array[i] position will return me an arraylist of integers.
thank you
You have to initialize each ArrayList in the array.
ArrayList[] solucao = new ArrayList[6];
for (int i = 0; i < solucao.length; i++)
solucao[i] = new ArrayList();
I actually thought you couldn't have an array of ArrayList. Apparently you can, but it must be non-generic. You should probably reconsider why you're doing this...
Arrays are just pointers or references. For each for them you have to create a new ArrayList object and store your data in it.
List[] solucao= new ArrayList[5];
for(int i=0;i<solucao.length;i++)
{
solucao[i] = new ArrayList();
solucao[i].add(yourObject);
}
ArrayList<Integer>[] solucao= new ArrayList[6];
Should be new ArrayList<Integer>[6]
Note an IDE would give you a warning about this. Next, initialize each element of the array (Java 7):
for(int i = 0; i < solucao.length; i++) {
solucao[i] = new ArrayList<>();
For store data first you have to create object.
ArrayList<Integer>[] ls = new ArrayList[7];
for (int i = 0; i < ls.length; i++) {
ls[i] = new ArrayList<Integer>();
for(int j = 0 ; j<i ;j++){
ls[i].add(j);
}
System.out.println(ls[i].size());
}