我是Java的新手。如何编写与以下C代码等效的Java代码。
void Swap(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
我是Java的新手。如何编写与以下C代码等效的Java代码。
void Swap(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
这里有一个技巧:
public static int getItself(int itself, int dummy)
{
return itself;
}
public static void main(String[] args)
{
int a = 10;
int b = 20;
a = getItself(b, b = a);
}
这里有一种使用位运算符XOR(^)仅用一行代码在Java中交换两个变量的方法。
class Swap
{
public static void main (String[] args)
{
int x = 5, y = 10;
x = x ^ y ^ (y = x);
System.out.println("New values of x and y are "+ x + ", " + y);
}
}
输出:
x和y的新值为10,5
简短的回答是:你无法这样做,因为Java没有指针。
但是,以下是类似的方法:
public void swap(AtomicInteger a, AtomicInteger b){
// look mom, no tmp variables needed
a.set(b.getAndSet(a.get()));
}
Collections.swap(List, int, int)
:Swaps the elements at the specified positions in the specified list.
(If the specified positions are equal, invoking this method leaves
the list unchanged.)
Parameters:
list - The list in which to swap elements.
i - the index of one element to be swapped.
j - the index of the other element to be swapped.
显然真正的目标是对一个int类型的数组进行排序。
使用Arrays.sort(int[])
即可实现一行代码解决:
int[] arr = {2,3,1,378,19,25};
Arrays.sort(arr);
检查输出:
System.out.println(Arrays.toString(arr));
// [1, 2, 3, 19, 25, 378]
这里是一个简单的辅助函数,用于交换整数数组中的两个位置:
public static void swap(final int[] arr, final int pos1, final int pos2){
final int temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
int aux = b; b = a; a = aux;
- helios对于任何原始数字类,包括 double
和 float
,请使用此一行代码:
a += (b - (b = a));
例如:double a = 1.41;
double b = 0;
a += (b - (b = a));
System.out.println("a = " + a + ", b = " + b);
输出为a = 0.0,b = 1.41
Integer
对象并不是按值传递的。它们根本没有被传递。将传递一个指向 Integer
对象的引用,且这个引用是按值传递的。在Java中,你只能传递引用和基本类型的值,永远不会传递对象! - Joachim Sauerpublic void swap(int[] a, int[] b) {
int temp = a[0];
a[0] = b[0];
b[0] = temp;
}
public void test() {
final int[] a = int[]{ 42 };
new Thread(new Runnable(){ public void run(){ a[0] += 10; }}).start();
while(a[0] == 42) {
System.out.println("waiting...");
}
System.out.println(a[0]);
}
强大的IntHolder怎么样?我喜欢任何带有omg名称的包!
import org.omg.CORBA.IntHolder;
IntHolder a = new IntHolder(p);
IntHolder b = new IntHolder(q);
swap(a, b);
p = a.value;
q = b.value;
void swap(IntHolder a, IntHolder b) {
int temp = a.value;
a.value = b.value;
b.value = temp;
}
片段-1
public int[] swap1(int[] values) {
if (values == null || values.length != 2)
throw new IllegalArgumentException("parameter must be an array of size 2");
int temp = values[0];
values[0]=values[1];
values[1]=temp;
return values;
}
代码段-2
public Point swap2(java.awt.Point p) {
if (p == null)
throw new NullPointerException();
int temp = p.x;
p.x = p.y;
p.y = temp;
return p;
}
使用方法:
int[] values = swap1(new int[]{x,y});
x = values[0];
y = values[1];
Point p = swap2(new Point(x,y));
x = p.x;
y = p.y;
Java使用传值方式。无法使用方法交换两个基本类型或对象。
虽然可以在整数数组中交换两个元素。
在Java中,您无法使用引用,因此交换函数是不可能的,但是您可以针对每个交换操作使用以下代码片段:
T t = p
p = q
q = t
其中T是p和q的类型
然而,通过重写属性可能会交换可变对象:
void swap(Point a, Point b) {
int tx = a.x, ty = a.y;
a.x = b.x; a.y = b.y;
b.x = t.x; b.y = t.y;
}