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
Related
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:
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);
}
This question already has answers here:
What does this for(:) mean in Java?
(2 answers)
Closed 6 years ago.
I am reading an example in my textbook (finding the path of a file recursively) and came across this format for a loop
for (File folderItem : dir.listFiles()){}
The only for loop I'm used to is
for (int i = 0; i < 10; i++){}
How does this loop work, and what is the logic behind it?
This is a new type of for loop (introduced in Java 5). It is used to iterate over some types of collections. It's basically identical to
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++)
{
File folderItem = files[i];
// Code
}
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.
This question already has answers here:
Arrays.asList() not working as it should?
(12 answers)
Closed 9 years ago.
So I'm just learning about arrays in Java (interesting stuff) but I'm having some problems getting my head around the contains() method.
I tried:
Random rn = new Random();
int first = 12;
int[] tab = new int[first];
for (int i = 0; i <= first - 1; i++) {
tab[i] = rn.nextInt(10);
Which seemed to work fine for filling in my Array, but then I tried a:
System.out.println(Arrays.asList(tab).contains(9));
And no matter what, even if I fill the array manually with 9's, it'll still only print up "false".
What am I doing wrong?
Try this
Integer[] tab = new Integer[]{9};
System.out.println(java.util.Arrays.asList(tab).contains(9));
I get
true