如何在J中编写基本的交换函数
这个问题已经在这里有了答案:
交换原语的Java方法 8个答案
我是Java新手。 如何编写等效于以下C代码的java。
void Swap(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
Melinda asked 2020-01-08T16:59:07Z
19个解决方案
86 votes
这是一个技巧:
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);
}
Eng.Fouad answered 2020-01-08T16:59:17Z
39 votes
排序两个整数
简短的答案是:您无法做到这一点,java没有指针。
但是您可以执行以下类似操作:
public void swap(AtomicInteger a, AtomicInteger b){
// look mom, no tmp variables needed
a.set(b.getAndSet(a.get()));
}
您可以使用各种容器对象(例如集合和数组或具有int属性的自定义对象)来执行此操作,但不能使用基元及其包装器(因为它们都是不可变的)执行此操作。 但是,我想使它成为单线的唯一方法是使用AtomicInteger。
顺便说一句:如果您的数据碰巧是一个列表,则一种更好的交换方式是使用Arrays.sort(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]
这是一个简单的辅助函数,用于交换一个int数组中的两个位置:
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;
}
Sean Patrick Floyd answered 2020-01-08T17:00:13Z
36 votes
这是一种使用按位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
Prateek Joshi answered 2020-01-08T17:00:41Z
9 votes
对于任何原始数字类(包括a = 0.0, b = 1.41和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
Oleg Mikhailov answered 2020-01-08T17:01:10Z
4 votes
Java中没有指针。 但是,“包含”对象的每个变量都是对该对象的引用。 要具有输出参数,您将必须使用对象。 就您而言,整数对象。
因此,您必须制作一个包含整数的对象,然后更改该整数。 您不能使用Integer类,因为它是不可变的(即其值不能更改)。
一种替代方法是让该方法返回一个数组或一对整数。
Sjoerd answered 2020-01-08T17:01:40Z
3 votes
那强大的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;
}
PowerApp101 answered 2020-01-08T17:02:00Z
2 votes
在这种情况下,有一种使用带有一个元素的数组的快速而肮脏的解决方案:
public 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]);
}
Landei answered 2020-01-08T17:02:25Z
2 votes
片段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;
Andreas_D answered 2020-01-08T17:02:56Z
2 votes
Java使用按值传递。 无法使用方法交换两个原语或对象。
尽管可以交换整数数组中的两个元素。
codaddict answered 2020-01-08T17:03:20Z
1 votes
您不能在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;
}
Ming-Tang answered 2020-01-08T17:03:50Z
1 votes
您必须内联。 但是,您实际上不需要Java中的该交换。
Bart answered 2020-01-08T17:04:09Z
1 votes
您的交换功能实际上是在两个内存中更改值。 现在,引用这些内存位的所有内容都将获得不同的值。
在Java中,实际上没有指针,因此这是行不通的。 相反,引用保留在对象上,并且您只能在对象内部进行更改。 如果需要在两个地方引用一个对象,以便可以在系统中传递相同的值并使事物对它们的更改做出反应,请尝试诸如存储库模式或依赖项注入之类的操作。
我们只能猜测为什么您需要用C编写此代码。我能提供的唯一建议是考虑对您要实现的对象的更改,最好在实际对象上添加一个方法,而不是拉出它们的内部构造,并且 而是调用该方法。 如果这对您没有帮助,请尝试发布调用代码,因为我们可能会对如何解决Java风格的实际问题有一个好主意。
Lunivore answered 2020-01-08T17:04:40Z
1 votes
Java是按价值传递的。 因此,按您的意思说是swap是不可能的。 但是您可以交换两个对象的内容,也可以内联。
fastcodejava answered 2020-01-08T17:05:00Z
1 votes
您可以使用或不使用临时变量来交换变量。
这是一篇文章,提供了多种方法来交换没有temp变量的数字:
[http://topjavatutorial.com/java/java-programs/swap-two-numbers-without-a-temporary-variable-in-java/]
Sekhar Ray answered 2020-01-08T17:05:28Z
0 votes
您可以轻松地自己写一个。
给出:
int array[]={1,2};
你做:
int temp=array[0];
array[0]=array[1];
array[1]=temp;
这样就完成了。 3行代码。
Yunus Seçgin answered 2020-01-08T17:06:01Z
0 votes
在Java中,无法使用指针进行交换。 但是,您可以通过传递包含两个对象的数组来实现交换。
代码如下:
public class Swap {
public static void swap(String [] a){
String temp;
temp = a[0];
a[0] = a[1];
a[1] = temp;
}
public static void main(String [] args){
String [] foo = new String[2];
foo[0] = "str1";
foo[1] = "str2";
swap(foo);
System.out.println("First value: "+ foo[0]);
System.out.println("Second value: "+ foo[1]);
}
}
输出:
First value: str2
Second value: str1
Nabin Bhandari answered 2020-01-08T17:06:30Z
0 votes
public class swaptemp {
public static void main(String[] args) {
String s1="10";
String s2="20";
String temp;
System.out.println(s1);
System.out.println(s2);
temp=Integer.toString(Integer.parseInt(s1));
s1=Integer.toString(Integer.parseInt(s2));
s2=Integer.toString(Integer.parseInt(temp));
System.out.println(s1);
System.out.println(s2);
}
}
user5962065 answered 2020-01-08T17:06:45Z
-1 votes
//here is also another answer:
class SwapDemo{
static int a=1, b=2 ;
public static void main(String [] args){
Swap swp = new Swap();
swp.swaps(x,y);
System.out.println( " a (was 1)now is " + a + " b (was 2) now is " + b);
}
}
class Swap{
void swaps(int c, int d){
SwapDemo f = new SwapDemo();
f.a = c;
f.a = d;
}
}
Tepken Vannkorn answered 2020-01-08T17:07:01Z
-2 votes
class Swap2Values{
public static void main(String[] args){
int a = 20, b = 10;
//before swaping
System.out.print("Before Swapping the values of a and b are: a = "+a+", b = "+b);
//swapping
a = a + b;
b = a - b;
a = a - b;
//after swapping
System.out.print("After Swapping the values of a and b are: a = "+a+", b = "+b);
}
}
Tepken Vannkorn answered 2020-01-08T17:07:16Z