Program output explanation HashSet [duplicate] - java

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.

Related

How to sort an arraylist of type class with respect to two elements(two columns) in it [duplicate]

This question already has answers here:
Sorting Java objects using multiple keys
(7 answers)
Closed 3 years ago.
I'm having an arraylist(arrList) of type 'class'(Info).The class 'Info' is having two properties called 'case' and 'step' of type long. I need to sort the arraylist in ascending order by comparing both the properties in class Info.
//scrambled list
arrList={[#case,#step],[2,1],[1,4],[2,2],[1,2],[1,3],[1,1],[2,3],[2,4],.....}
//Output I need
arrList={[#case,#step],[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[2,4],...}
I'll make it more clear.For example,
Case 1 is having Steps 1,2,3,4 &Case 2 is having Steps 1,2,3,4
So, here I need the arraylist like case and step sorted together([1,1] ,[1,2],[1,3]...)
I tried with the code given below.
for (int i = 0; i < arrList.size(); i++) {
for (int j = i+1; j < arrList.size(); j++) {
long tmp1Case=arrList.get(i).getCase_ID();//case id value --i
long tmp1Step=arrList.get(i).getStep_ID();//step id value --i
long tmp2Case=arrList.get(j).getCase_ID();//case id value --j
long tmp2Step=arrList.get(j).getStep_ID();//step id value --j
if (tmp1Case>=tmp2Case && tmp1Step>tmp2Step ) {
Info tmp = new Info();
tmp = arrList.get(i);
arrList.get(i).setStep_ID(arrList.get(j).getStep_ID());
arrList.get(i).setCase_ID(arrList.get(j).getCase_ID_ID());
arrList.get(j).setStep_ID(tmp.getStep_ID());
arrList.get(j).setCase_ID(tmp.getCase_ID());
}
}
}
//printing the arraylist ...
Output is wrong. Help me out. Thanks in advance
The easiest way to do it is to make Info implement comparable. Then you just have to implement the compareTo function that handles your cases and will return 0 if they are the same 1 if the the comparison is greater than and -1 if it's less than. Than you can just use Collections.sort(arrList) https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
Assuming you have java 8 or higher and further assuming you have the getters for both properties:
arrList.sort(Comparator.comparing(Info::getCase_ID).thenComparing(Info::getStep_ID));

Is there a method to create objects with a name that is equal to the current state of a variable [duplicate]

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

How can I get a variable by adding strings [duplicate]

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

How to copy all the elements in an array list, then paste them to the array list? Example provided [duplicate]

This question already has answers here:
How to append the contents of a list at the end of the other list?
(3 answers)
Closed 8 years ago.
I want to copy all elements of ArrayList to the back of the ArrayList. For example, in my ArrayList, there are {1,2,3,4} and I want it to be like this --> {1,2,3,4,1,2,3,4}.
How do I do it?
for (int pos = 0; pos < hand.size (); pos ++)
{
hand.add (hand.get(pos));
}
This gives me an error saying out of memory...
Is there any way to make it work?
Thank you
Your loop looks roughly like:
is pos < hand.size()?
if so, add something to hand
pos++
which means that each time through the loop, pos increases, but hand.size() also increases, and so on. You get the out-of-memory error because the loop runs infinitely, and the list gets too big.
One way to do this:
int length = hand.size();
for (int pos = 0; pos < length; pos++) {
hand.add(hand.get(pos));
}
How about
hand.addAll(new ArrayList<>(hand))
Every time you add an element, the size increases, so the loop never terminates. A simple fix is to save the size in a variable before the loop:
int size = hand.size()(
for (int pos = 0; pos < size; pos ++)
{
hand.add (hand.get(pos));
}
But the simplest way to do it is:
hand.addAll(hand); // Note: It is safe to call addAll() on itself

Arrays and searching in Java [duplicate]

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

Categories