This question already has answers here:
Why does my ArrayList contain N copies of the last item added to the list?
(5 answers)
Closed 5 years ago.
Okay so I've done some research on this and am stuck. I understand now that the list just holds pointers to the hashmap so I end up with 5 pointers to the same value. I cannot for the life of me think of any way to get around this problem. How can i store the values and keep them? I tried using String.valueOf() but that is a pointer as well apparently.
List<HashMap<String,String>> tbcPackages = new ArrayList<HashMap<String,String>>();
JSONObject g = new JSONObject();
for(int i = 0; i <5 ; i++){
g.clear();
System.out.println("clear? : " + g);
g.put("test", i);
tbcPackages.add(g);
System.out.println(tbcPackages);
}
The problem occurs because you are using same object "JSONObject g". At every iteration of loop, create a new object. The modified code given below:
List<HashMap<String,String>> tbcPackages = new
ArrayList<HashMap<String,String>>();
for(int i = 0; i <5 ; i++){
JSONObject g = new JSONObject();
g.clear();
System.out.println("clear? : " + g);
g.put("test", i);
tbcPackages.add(g);
System.out.println(tbcPackages);
}
Related
This question already has answers here:
NullPointerException when Creating an Array of objects [duplicate]
(9 answers)
Closed 2 years ago.
I'm using an Array of ArrayLists of Integer in Java but it can't run. Every time I get the message
"Note: Goal.java uses unchecked or unsafe operations." and
"Note: Recompile with -Xlint:unchecked for details." after compile and when I want to run it message "Exception in thread "main" java.lang.NullPointerException" appears.
the code is (All is in main method) :
int v = 8;
ArrayList<Integer>[] adj = new ArrayList[11];
adj[0].add(21);
and the error belongs to the last line.
searched a lot but nothing ! plz help, stuck for hours now.
According to the comment section, people are right about the null elements in your array of arraylists. To resolve the NullPointerException you should initialize elements of the array right after its declaration and instantiation.
Here's the modified code below that works:
ArrayList<Integer>[] adj = new ArrayList[11];
for (int i = 0; i < 11; i++)
adj[i] = new ArrayList<Integer>();
adj[0].add(21);
You should first initialize all indices of the array:
int v = 8;
ArrayList<Integer>[] adj = new ArrayList[11];
for(int i=0; i<adj.length; i++) {
adj[i] = new ArrayList<>();
}
adj[0].add(21);
This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 3 years ago.
I'm trying to create a grid of cells for a cellular automata in processing, but I get an error message telling me it was expecting SEMI and it found cell is there anything else I can do?
for (int i = 0; i < 12960; i = i+1)
{
x = x+100;
if (x > width)
{
y = y+100;
x = 0;
}
cell cell[i] = new cell(x, y);
I would expect the result of this to create 12960 objects that each have the name cell[x] where x is an integer from 0 to 12960. However, I get an error message reading:
expecting SEMI, found 'cell'
Syntax error, maybe a missing semicolon?
is there any way to get a result like the one I want with a different method?
this is not related to the name of the object being the same as the class as I have tried it with a different name.
I don't believe you can dynamically create object names like that in Java, try using a HashMap instead
Map<String, cell> cellList = new HashMap<String, cell>();
for (int i = 0; i < 12960; i = i+1)
{
x = x+100;
if (x > width)
{
y = y+100;
x = 0;
}
cellList.put("cell"+i, new cell(x, y));
}
This question already has answers here:
How to add arrays to ArrayList?
(3 answers)
Closed 7 years ago.
I have a need create an ArrayList to collect positions of some points.
ArrayList<int[]> collection = new ArrayList<int[]> ;
//the position has 2 coordinations.
int[] location = new int[2]
//add first position a,b
location[0] = a;
location[1] = b;
collection.add(location);
//add second position c,d
location[0] = c;
location[1] = d;
collection.add(location);
When I try to display the collection, all the elements inside are exactly the same as the last one was added (in this case: [c,d])
How do I add the element to my ArrayList properly in this case ? Thank you very much
ArrayList<int[]> collection = new ArrayList<int[]> ;
//the position has 2 coordinations.
int[] location = new int[2]
//add first position a,b
location[0] = a;
location[1] = b;
collection.add(location);
//add second position c,d
location = new int[2]; //<-here
location[0] = c;
location[1] = d;
collection.add(location);
Actually i would recommend you encapsulate your location as a POJO, for better flexibility and usability.
You need to create a new array forma each pair of coirdinates you add to the ArrayList
You can accomplish this easily with
collection.add(new int[]{a,b});
collection.add(new int[]{c,d});
This question already has answers here:
Dynamic variable names Java
(3 answers)
Closed 7 years ago.
I have for example:
String invslot_ = "invslot_";
int i = 0;
Now I have a while(i < 44) loop and I want it
every time it loops to "add" invslot_ and i.
(Yes I could do invslot_ + i !)
But I want that infslot_ + 1 is recognized as a variable I have defined earlier!
I hope you understand what I mean, please answer if you have an idea.
Map<String, Number> vars = new HashMap<>();
vars.put("invslot_1", 3267);
for (int i = 0; i < 44; i++)
if (!vars.contains("invslot_" + i)) // put only if "var" is not already "assigned"
vars.put("invslot_" + i, i);
System.out.println(vars.get("invslot_1")); // 3267
System.out.println(vars.get("invslot_11")); // 11
This question already has answers here:
Java: Different outputs when add/remove short and integer elements in a Set
(3 answers)
Closed 8 years ago.
Set<Short> set = new HashSet<Short>();
short i = 0;
for (i = 0; i < 100; i++) {
set.add(i);
set.remove(i-1);
}
System.out.println(set.size());
When we run above program output comes 100.
I understood it that As we are inserting short values in set and trying to remove integer value. So It's not removing from set.
But if we modify remove statement as below
set.remove(i);
Output coming 0. Any idea?
While you are removing it from the set, i-1 is treated as object and need to be typecast to shore to remove correct value from the set. set.remove((short)(i - 1))
Set<Short> set = new HashSet<Short>();
short i = 0;
for (i = 0; i < 100; i++) {
set.add(i);
set.remove((short)(i - 1));
}
System.out.println(set.size());
Try this code its giving output as 1.