Java changes the variable without me doing so [duplicate] - java

This question already has answers here:
How to copy Java Collections list
(18 answers)
How do I clone a generic List in Java?
(14 answers)
Closed 2 years ago.
So I wanna compute a covexhull but in the I want the right and left sets seperately. I cannot understand what Java is doint right here exactly.
So I first call the hullset function for the right part and then put it in rightCov (at that point right set has 3 points). After that I can hullset for the left set . The problem is that even though I have "saved" the 3 points of the right set after the second call of the hullset,for the left set only,the variable rightCov takes all the 5 points of the covexhull without me using it again. The calls are below.
hullset(A, B, rightSet, covexHull);
ArrayList<Point> rightCov=covexHull;
hullset(A,B,leftSet, covexHull);

ArrayList<Point> rightCov=covexHull; assigns rightCov the same reference as covexHull so they both point to the same list in memory. You should use addAll methos to copy the elements from one array into another.

Related

How do you sort an ArrayList of entities by their Y-value? [duplicate]

This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 3 years ago.
I am making a game where I can spawn soldiers with a button click. All of these soldiers will be added in an ArrayList named entity_walking_right_array. I also have another ArrayList for the entities which are walking left.I have another class where the x, y position, the animation etc. of the soldier is determined or drawn. All soldiers have a random Y value between the screen height and 450 pixels.
When I spawn the soldiers some of them will logically have a higher y value or y position than others but when those are created later, than soldiers with a lower y value, they will be rendered above them. That leads to soldiers being rendered above soldiers when they logically would be behind them. (For example the feets are in the face of the lower y value soldier)
I then render the soldiers like that:
for (entity_walking_right blue : entity_walking_right_array) {
blue.render(game.batch);
}
I figured out that when I use the y value of those soldiers ( entity_walking_right.getY(); is a float) to sort the ArrayList then they should be rendered in the right order. At least I am hoping that is the right way to do it.
I have already tried using entity_walking_right_array,sort(...), Collections.sort or some of those Lambda functions which are completely new to me. I've also tried using compareTo (I unfortionatly cannot find the question anymore) but it gave me an error which basically said that compareTo just does not exist as a function.
Any code that I've found did not really help me and I am trying to get this problem solved for 2 days now which is the reason why I am writing the question.
Edit:
I have found the question.
Sort ArrayList of custom Objects by property
When I try to use
Collections.sort(entity_walking_right_array,
(o1, o2) -> o1.getY().compareTo(o2.getY()));
it simply says "Cannot resolve method 'compareTo(float)'.
When comparing a primitive type you can't use compareTo as in the linked question so you need to write your own or used a predefined one
entity_walking_right_array.sort(Comparator.comparingDouble(entity_walking_right::getY));
or
entity_walking_right_array.sort(Comparator.comparingDouble(entity_walking_right::getY).reversed());
depending on sort order.
And please use java standard naming, entity_walking_right -> entityWalkingRight
it simply says "Cannot resolve method 'compareTo(float)'.
You need to use method compare instead of compareTo and pass as parameters the two objects you want compare.
https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
Collections.sort(entity_walking_right_array,
(o1, o2) -> compare(o1.getY(), o2.getY()));

java.io.File.list() giving different order as the folders created sequentially [duplicate]

This question already has answers here:
Best way to list files in Java, sorted by Date Modified?
(21 answers)
Closed 9 years ago.
java.io.File.list() giving different order as the folders created sequentially
While creating the Temporary folders with name as
"Temp" + auto-incremented number
e.g. Temp1, Temp2, Temp3...Temp10, Temp11 and so on.
But when I use java.io.File.list() to get the folder list to further processing in order as the folder created,
but its giving as Temp1, Temp10, Temp11...Temp19, Temp2, Temp20, Temp21 and so on.
I also tried Arrays.sort(files); but no change in order of folder list
Please tell how to get the same order list as the folders created sequentially?
It's because the way how java compares strings by default in java (lexicographic order).
You can define your own comparator for File objects and sort.

Initializing a 2D-array with zero in JAVA [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Any shortcut to initialize all array elements to zero?
How to Initializing a normal 2D array with zeros(all shells should have zero stored) without using that old looping method that takes lots of time .My code wants something that should take very less time but via looping method my time limit exceeds.
The important thing to realise here is that if you're storing primitives then the default value is zero (scroll down to the Default Values section). This applies to array initialisation as well as simple variable initialisation, and consequently you don't have to initialise the array contents explicitly to zero.
If you have to reset an existing structure, however, then you'd have to loop through the structure, and you may be better off initialising a new instance of your array structure.
Note that if you're creating an array or arrays, obviously you have to initialise the first array with the contained array.

How can I copy an array by value in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make copy of array Java
I'm a beginner at Java and I need to copy the contents of one array into another variable. However, Java always passes the array by reference instead of by value.
Here's what I mean if that was confusing:
int test[]={1,2,3,4};
int test2[];
test2=test;
test2[2]=8;
for(int i=0;i<test2.length;i++)
System.out.print(test[i]); // Prints 1284 instead of 1234
In this example, I don't want the value of test to change. Is this possible without using any of the more advanced features of Java such as ArrayList and Vectors?
Edit: I tried System.ArrayCopy and test.clone(), but they still don't seem to work.
Here's my actual code:
temp_image=image.clone();
for(int a=0;a<image.length;a++)
for(int b=0;b<image[0].length;b++)
image[a][b]=temp_image[image.length-1-a][b];
Basically I'm trying to flip the "image" upside down. Is there an error somewhere in the code?
You need to clone your array.
test2=test.clone();
Starting in Java 6 you can use Arrays.copyOf:
test2 = Arrays.copyOf(test, test.length);
For what you're looking to do, test.clone() is fine. But if you wanted to do a resize, copyOf allows you to do that. I think in terms of performance it
System.arraycopy would give even more options if you needed them.
Because test and test2 are both pointers to the same array, you are changing the value of both test and test2 with your statement test2[2]=8
A solution would be to copy the contents of test into test 2 and change the value at the specific index of test2.
for (int i=0,i<test.length,i++)
test2[i]=test[i]
//Now both arrays have the same values
test2[2]=8
for (int j=0,j<test.length,j++)
System.out.print(test[i])
System.out.println()
System.out.print(test2[i])
Will output
1 2 3 4
1 2 8 4

how does intellij calculate the 4 digit ids of object instances? [duplicate]

This question already has an answer here:
Deciphering variable information while debugging Java
(1 answer)
Closed 4 years ago.
When using Intellij's debugger, the variables in scope are displayed using a 4 digit identifier, marked in red in the following screenshot.
This identifier seems to be calculated based on the object's identity.
What exact code is used to get the 4 digit number for a given object instance?
I don't think you should rely on that id being calculated in any special way. It's internal to IDEA (or JVM) and I don't think it holds any relevance except to track objects during execution.
However, I find it useful to name objects during debug. I believe the shortcut is F11 (click once on the object in debug window first) and just give it a name that is meaningful for you during debugging. That object will always hold that name during the current session of debugging.

Categories