Dota 2 swap function in java

Dota 2 swap function in java

Dota 2 swap function in java

How to write a basic swap function in Java [duplicate]

I am new to java. Dota 2 swap function in java How to write the java equivalent of the following C code.

19 Answers 19

Here is one trick:

Sorting two ints

The short answer is: you can’t do that, java has no pointers.

But here’s something similar that you can do:

You can do this with all kinds of container objects (like collections and arrays or custom objects with an int property), but just not with primitives and their wrappers (because they are all immutable). But the only way to make it a one-liner is with AtomicInteger, I guess.

BTW: if your data happens to be a List, a better way to swap is to use Collections. swap(List, int, int) :

Sorting an int[] array

apparently the real objective is to sort an array of ints. That’s a one-liner with Arrays. sort(int[]) :

To check the output:

And here is a simple helper function to swap two positions in an array of ints:

Here’s a method to swap two variables in java in just one line using bitwise XOR(^) operator.

Output:

New values of x and y are 10, 5

Use this one-liner for any primitive number class including double and float :

Output is a = 0.0, b = 1.41

There are no pointers in Java. Dota 2 swap function in java However, every variable that “contains” an object is a reference to that object. To have output parameters, you would have to use objects. In your case, Integer objects.

So you would have to make an object which contains an integer, and change that integer. You can not use the Integer class, since it is immutable (i. e. its value cannot be changed).

An alternative is to let the method return an array or pair of ints.

What about the mighty IntHolder? I just love any package with omg in the name!

In cases like that there is a quick and dirty solution using arrays with one element:

Of course your code has to work with these arrays too, which is inconvenient. The array trick is more useful if you want to modify a local final variable from an inner class:

Java uses pass-by-value. It is not possible to swap two primitives or objects using a method.

Although it is possible to swap two elements in an integer array.

You cannot use references in Java, so a swap function is impossible, but you can use the following code snippet per each use of swap operations:

where T is the type of p and q

However, swapping mutable objects may be possible by rewriting properties:

You have to do it inline. But you really don’t need that swap in Java. Dota 2 swap function in java

Your swap function is essentially changing the values in two pieces of memory. Anything referencing those bits of memory will now get different values.

In Java there aren’t really pointers, so this won’t work. Instead, references are held on objects, and you can only change stuff inside the objects. If you need to reference one object in two places, so that you can pass the same values around the system and have things react to them changing, try something like the repository pattern or dependency injection.

We can only guess at why you needed this code in C. The only advice I can give is to think about the changes to the objects which you want to achieve, preferably add a method on the actual objects rather than pulling their internals out, and call that method instead. If this doesn’t help you, try posting the calling code as we’ll probably have a good idea of how to solve the real problem Java-style.

Dota 2 swap function in java

Leave a comment