Array of arraylists - java

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());
}

Related

Is there any option to create few ArrayLists automaticly with loop?

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.

Creating x ArrayLists

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});

array of arraylist type error

void radix(int[] arr, int maxDigit){
int exp = 1;
for (int i=0; i<maxDigit; i++) {
ArrayList[] bucket = new ArrayList[10];
for (int n=0; n<10; n++){
bucket[n] = new ArrayList<Integer>();
}
for (int j=0; j<arr.length; j++){
bucket[(arr[j]%exp)%10].add(arr[j]);
}
int ind=0;
for (int k=0; k<10; k++){
for (int n : bucket[k]){
arr[ind] = n;
ind++;
}
}
exp*=10;
}
}
above is my attempt for a radix sort in java.
I keep having an error at this for loop:
for (int n : bucket[k])
Saying that Object can't be converted to int.
However, I declared the array to have an arrayList as its element,
so bucket[k] should be an arraylist.
Can anyone please explain why this happens and how I could get around this problem?
I would appreciate any help. Thanks
You have an ArrayList<Integer>. Try using for(Integer n : bucket[k])
You also need to provide a type for ArrayList. Try ArrayList<Integer>[] bucket = new ArrayList[10];
ArrayList[] bucket = new ArrayList[10];
This does not have the type parameter.
And you should avoid using inflexible, low-level constructs like arrays anyways.
Try to actually express your intents in your code with identifiers and to use first-class objects for all of your key concepts.
You have an ArrayList not ArrayList.
Change:
for(int n : bucket[k])
To:
for(Integer n : bucket[k])
You also need to type your ArrayList declaration with:
ArrayList<Integer>[] bucket = new ArrayList<Integer>[];

ArrayList of Arraylist a remove function affects all Arraylists

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));

How to create a 2D ArrayList?

I'm trying to use Java to create a 2-dimensional array. The size of rows is known, while the size of columns is unknown. Here is my code and it doesn't work. Could anyone give me some idea?
ArrayList<Integer> paths[];
paths = new ArrayList[2];// 2 paths
for (int i=0; i<2; ++i)
paths[i].add(1); // add an element to each path
Initialize the array element before adding to it. Put the initialization into the for loop:
#SuppressWarnings("unchecked")
ArrayList<Integer>[] paths = new ArrayList[2];
for (int i=0; i<2; ++i) {
paths[i] = new ArrayList<Integer>();
paths[i].add(1);
}
This way you can avoid the NullPointerException.
I would recomend this
ArrayList<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
for (int i=0; i<2; ++i)
paths.add(new ArrayList<Integer>());
This is a "2d" ArrayList:
ArrayList<ArrayList<Integer>> paths = new ArrayList<>();
And here is the non-diamond operator version for Java < 1.7:
ArrayList<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();

Categories