implementing swap for integers in java

M

Madhur Ahuja

Hello
Since java doesnt supports pointers, how do you write a function which swaps
the contents of 2 integers.

--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
 
D

David Hilsee

Madhur Ahuja said:
Hello
Since java doesnt supports pointers, how do you write a function which swaps
the contents of 2 integers.

Use a different class instead of Integer or int.

public class IntHolder {
private int value;
// constructors, etc
public void swap( IntHolder i ) {
int temp = value;
value = i.value;
i.value = temp;
}
}

It's probably not worth it if that's the only reason for the class to exist.
 
T

Tor Iver Wilhelmsen

Madhur Ahuja said:
Since java doesnt supports pointers, how do you write a function which swaps
the contents of 2 integers.

This is the wrong approach to the problem: The _real_ question is: Why
do you want to swap the numbers, and how can you solve that problem in
Java?
 
A

ak

Since java doesnt supports pointers, how do you write a function which
swaps
the contents of 2 integers.

never had such problem.
Why do you want to do it?
Better you forget c, if you want to program in java.
 
M

Michael Borgwardt

Madhur said:
Since java doesnt supports pointers, how do you write a function which swaps
the contents of 2 integers.

What do you need it for? The only thing I can think of is sorting
an array, and there you just pass the array and the indices to swap
instead of the contents.
 
B

Bryce

Hello
Since java doesnt supports pointers, how do you write a function which swaps
the contents of 2 integers.

How do you want to swap them? I can find no reason to abstractly swap
two integers.

Now if they were part of a List, that's a different story. Swaping
positions in a list is easy...
 
L

Liz

Madhur Ahuja said:
Hello
Since java doesnt supports pointers, how do you write a function which swaps
the contents of 2 integers.

--
Winners dont do different things, they do things differently.

Madhur Ahuja
India
I think Integer is immutable, do you mean change the
reference?
 
V

Virgil Green

Liz said:
I think Integer is immutable, do you mean change the
reference?

The OP was talking about little 'i' integers, not big 'I' Integers.

- Virgil
 
M

Madhur Ahuja

Hello

Well, I am learning java, and doing assignments on data structures like
sorting, searching which requires quite a bit
of swapping of integers.


--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
 
L

Larry Coon

Madhur said:
Well, I am learning java, and doing assignments on data structures like
sorting, searching which requires quite a bit
of swapping of integers.

Pointers provide no advantage in this type of operation.
The simple, typical way of doing it is to use a temp
variable. For example, with ints i and j:

int temp = i;
i = j;
j = temp;

If the idea of temp variables makes your skin crawl:

i ^= j;
j ^= i;
i ^= j;

I'd also suggest that Java probably offers features that do
what you want to do (see the Collections framework), but if
you're learning it's probably a good idea to have a little
experience implementing these types of things yourself.


Larry Coon
University of California
 
J

Jim Cochrane

Hello

Well, I am learning java, and doing assignments on data structures like
sorting, searching which requires quite a bit
of swapping of integers.

If you take the sorting example, a common interface for sorting
(especially in CS classes) is a procedure that takes an array argument and
sorts the specified array. There is no problem with swapping integers (or
whatever you're sorting) in this case, since arrays are not immutable.
In other words, int arguments are not required with this interface,
so the problem you mentioned in your original post does not occur.

If you want to learn more about the issue you're bringing up, you might
try coming up with an example of a situation where you feel the problem
does occur. There are some very knowledgeable and helpful geeks who
post here and you're likely to get some very helpful responses.
 
L

Liz

Jim Cochrane said:
If you take the sorting example, a common interface for sorting
(especially in CS classes) is a procedure that takes an array argument and
sorts the specified array. There is no problem with swapping integers (or
whatever you're sorting) in this case, since arrays are not immutable.
In other words, int arguments are not required with this interface,
so the problem you mentioned in your original post does not occur.

If you want to learn more about the issue you're bringing up, you might
try coming up with an example of a situation where you feel the problem
does occur. There are some very knowledgeable and helpful geeks who
post here and you're likely to get some very helpful responses.

Here is a swap routine that I use when
sorting an array of doubles, you can
just change double to int and you're done


private static void swap(double[][] array, int row1, int row2) {
// swap two rows
for (int col = 0; col < array[0].length; col++) {
double tmp = array[row1][col];
array[row1][col] = array[row2][col];
array[row2][col] = tmp;
}
}
 
M

Michael Borgwardt

Jim Cochrane said:
If you take the sorting example, a common interface for sorting
(especially in CS classes) is a procedure that takes an array argument and
sorts the specified array. There is no problem with swapping integers (or
whatever you're sorting) in this case, since arrays are not immutable.

It's not an issue of mutability, the OP wants pass-by-reference, and Java
simply doesn't do that. But it's still no problem, you just have to give
the swap method access to the array itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top