使用两种方法创建一个名为 Flip 的类:
public float[] swap(float[] pair)
交换方法应采用包含两个浮点数的数组。 它应该返回一个数组,其中包含交换位置的相同两个数字。 例如,如果我像这样调用您的方法交换:
float[] x = { 3.14, 2.71 }
float[] y = swap(x);
Ln.p(y[0] + “, “ + y[1]);
调用您的交换方法的方法应该打印出2.71, 3.14
.
public double[] reverse(double[] reverse, int length)
反向方法应采用包含任意数量的双精度值(在长度上指定)的数组。 它应该以相反的顺序返回一个包含所有这些数字的数组。 您不必使用 for 循环,但这是编写此方法的最简单方法。 如果我像这样反向调用您的方法:
double[] a = { 0.33, 0.66, 0.25, 0.5, 0.75 }
double[] b = reverse(a, 5);
for (int i = 0; i < 5; i++) Ln.p(b[i] + “ “);
调用反向方法的方法应该打印出0.75 0.5 0.25 0.66 0.33
。
---所以我有第一个用于浮子,但我正在寻找第二部分的起点或方向。
import java.util.Scanner;
public class Flip
enter code here{
public float[] swap(float[] pair)
{
float x = 0;
float y = 0;
int xi = Float.floatToIntBits(x);
int yi = Float.floatToIntBits(y);
xi = yi - xi;
yi = yi - xi;
xi = xi + yi;
x = Float.intBitsToFloat(xi);
y = Float.intBitsToFloat(yi);
System.out.printf("My method calling your swap method is...." +x,y);
return null;
}
public double[] reverse(double[] reverse, int lenght)
{
double[] a = { 0.33, 0.66, 0.25, 0.5, 0.75 };
double[] b = reverse(a, 5);
for (int i = 0; i < 5; i++) Ln.p(b[i] + “ “);
return reverse;
}
}
要在大小为 2 的数组中交换到 elemtens,我会这样做:
float[] array = {2.7, 3.2};
float x = array[1];
array[1] = array[0];
array[0] = x;
另外不要忘记实际返回交换的数组!
要反转整个列表或数组,我建议在集合中使用 reverse() 方法。看看http://www.tutorialspoint.com/java/util/collections_reverse.htm
如果要使用自己的方法执行此操作,请尝试将输入数组中的值复制到具有相同大小的输出数组中,但反向复制:
float[] input = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
float[] out = new float[input.length];
for(int i=0;i<input.length; i++) {
out[i]=input[input.length-i-1];
}