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));
Related
I have the following Java code:
List<List<Integer>> list1 = new ArrayList<List<Integer>>(rankings);
for (int i = k + 1; i < rankings.size(); i++) {
for (int j = 0; j < rankings.get(0).size(); j++) {
int index = rankings.get(i).indexOf(j);
list1.get(i).set(index, map.get(j));
}
}
// ...
int newrank = sort(list1.get(i), 0, list1.get(i).size() - 1); // sorts list1
list1 came out sorted, but rankings came out sorted as well. How can I prevent this?
All I wanted to do is to create a duplicate of rankings so that the original copy won't be affected while I sort the temporary, copied array.
Thanks in advance.
new ArrayList<>(list) copies the reference here, not cloning the objects, every amends made in one element will affect both lists.
You can add the elements manually to clone it:
for (List<Integer> intList: rankings) {
List<Integer> someIntCopy = new ArrayList<>();
someIntCopy.addAll(intList);
list1.add(someIntCopy);
}
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 am trying to create a list using the code below, that stores random binary strings. But everytime I print the string elements of list, all the string elements that are printed are the same.
ex- 101 101 101 101. How do I make it work?
ArrayList<String[]> coded = new ArrayList<String[]>();
Random rand = new Random();
for(int j=0; j<4;j++){
for (int i=0; i<3;i++){
rand1 = (rand.nextInt(4)+0)%2;
x1[i]= "" + rand1;
}
coded.add(x1);
}
You have only declared one x1 array (somewhere), so in essence you're just adding a bunch of references to the same array to the list. The array will contain the values that were inserted during the last iteration.
Declare the array inside the loop to fix the issue.
for (int j=0; j<4; j++){
String[] x1 = new String[3];
for (int i=0; i<3; i++){
rand1 = (rand.nextInt(4) + 0) % 2;
x1[i]= "" + rand1;
}
coded.add(x1);
}
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());
}
I have an arraylist populated by four elements, the order of which is random (they are put here by random from another arraylist). I then have a for loop that repeats 10 times, at the end of each repetition I use the clear methods to clear all the elements of the arraylist. However, when I start a new repetition, I would like to repopulate my arraylist with the old (previously worked with) elements that were members of the list in the previous repetition, so that I can use the elements again. And I would like to repeat that until I get out of my 10-repetition for loop. Is there any way to achieve this at all?
Code in addition to my question:
ArrayList<String> answerPegs = new ArrayList<String>();
// add element to ArrayList
ArrayList<String> mySecretAnswer = new ArrayList<String>();
for (int n = 4; n > 0; n--)
{
//populate mySecretAnswer with elements from answerPegs
}
ArrayList<String> clone1 = mySecretAnswer;
for (int q = 0; q < 10; q++) {
for (o = 0; o < 4; o++)
{
}
// called clear() method here
} // END OF 10-ROW LOOP
I would suggest simply having 2 lists - keep a pristine copy of the original list, and then iterate over + clear a copy of that list.
public void doRepetitions(List<Object> original)
{
for( int i=0; i<10; i++ )
{
List<Object> working = new ArrayList<Object>( original );
doStuffWithList(working);
}
}
Edit:
Since you've posted your code, I can give a more specific answer:
You can change your clone to be:
ArrayList<String> clone1 = new ArrayList<String>(mySecretAnswer);
And then move that to be inside your for loop:
for (int q = 0; q < 10; q++)
{
ArrayList<String> clone1 = new ArrayList<String>(mySecretAnswer);
// ....
}
Could you use 2 loops nested and just have the inner loop be the for loop 10 times then clear once at the end