提问人:Talatus 提问时间:11/12/2023 更新时间:11/12/2023 访问量:91
尝试交换第一个和最后一个数组元素的值
Trying to swap values of the first and the last array elements
问:
void swapEven(int[] 数组)
在给定的整数数组中,如果交换的值都是偶数,则交换第一个和最后一个数组元素、第二个和倒数第二个数组元素的值,依此类推。
请注意,输入数组可能为空。
例子 代码示例:
... int[] 数组 = new int[]{ 10, 5, 3, 4 }; .... 输出:
[4, 5, 3, 10]
我的代码:
public class FirstProgram {
public static void main(String[] args) {
int[] myArray = new int[]{100, 2, 3, 45, 33, 8, 4, 54};
swapEven(myArray);
}
public static void swapEven(int[] array) {
// TODO: Implement this method.
int temp, firstNumIndex, secondNumIndex, counter = 1;
if (array != null) {
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
firstNumIndex = i;
if (array[array.length - counter] % 2 == 0) {
secondNumIndex = array.length - counter;
temp = array[firstNumIndex];
array[firstNumIndex] = array[secondNumIndex];
array[secondNumIndex] = temp;
}
}
counter++;
}
} else {
array = new int[]{};
System.out.println(Arrays.toString(array));
}
}
}
试图弄清楚为什么它给我相同的数组作为输入。它不交换数组。 我是否返回了一个错误的数组,我是否应该创建第二个数组来添加值,然后重新调整?
答:
如果遍历数组的整个长度,则所有偶数元素将被交换两次(这会将它们全部返回到其原始位置)。
相反,您应该遍历数组的一半。您可以使用两个索引从前方和后方跟踪位置,当它们相互通过时停止。
for (int i = 0, j = array.length - 1; i < j; i++, j--) {
if (array[i] % 2 == 0 && array[j] % 2 == 0) {
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
你差不多拥有它了!!一旦你交换了偶数值,你就重新交换了它们。因此,只需遍历一半的数组即可。如果你把
System.out.printf("Swapping %d with %d%n",
array[firstNumIndex], array[secondNumIndex]);
就在你开始交换之前。要修复它,请将以下内容更改为:for loop
for (int i = 0; i < array.length/2; i++) {
这是另一种形式。
你只需要一个常规的for循环,就像你所做的那样。
- 在数组中途迭代。
left
- 根据 的值将 最右边的位置分配给 。
right
left
- 检查以确保两者都是偶数值。
- 如果是这样,请交换它们。
请注意,这适用于偶数和奇数长度数组。
int[] arr = {1,2,3,4,5,6,7,8,9,10,11};
swapEven(arr);
System.out.println(Arrays.toString(arr));
指纹
[1, 10, 3, 8, 5, 6, 7, 4, 9, 2, 11]
方法
public static void swapEven(int[] array) {
if (array == null) {
System.out.println("Array is null");
return;
}
for (int left = 0; left < array.length / 2; left++) {
int right = array.length - left - 1;
if (array[left] % 2 == 0 && array[right] % 2 == 0) {
int temp = array[left];
array[left] = array[right];
array[right] = temp;
}
}
}
注意。如果你只需要反转一个数组,你可以省略对偶数值的检查。
"...在给定的整数数组中,如果交换的值都是偶数,则交换第一个和最后一个数组元素、第二个和倒数第二个数组元素的值,依此类推。..."
下面是一个示例。
您只需要遍历数组的一半。
使用 (n − 1) − i 访问相反的索引。
void swap(int[] a) {
for (int i = 0, t, n = a.length, m = n / 2; i < m; i++) {
if (a[i] % 2 == 0 && a[n - 1 - i] % 2 == 0) {
t = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = t;
}
}
}
或者,使用流。
void swap(int[] a) {
int n = a.length;
IntStream.range(0, n / 2)
.forEach(
x -> {
if (a[x] % 2 == 0 && a[n - 1 - x] % 2 == 0) {
int t = a[x];
a[x] = a[n - 1 - x];
a[n - 1 - x] = t;
}
});
}
而且,如果您使用的是 List,则可以使用 Collections#swap 方法。
void swap(List<Integer> l) {
for (int i = 0, n = l.size(), m = n / 2; i < m; i++)
if (l.get(i) % 2 == 0 && l.get(n - 1 - i) % 2 == 0)
Collections.swap(l, i, n - 1 - i);
}
输出
[4, 5, 3, 10]
[54, 4, 3, 45, 33, 8, 2, 100]
问题似乎出在数组为空时初始化的方式上。不应将新数组分配给参数,而应处理输入数组为空的情况。您还可以简化交换逻辑。您可以执行以下操作:
import java.util.Arrays;
public class FirstProgram {
public static void main(String[] args) {
int[] myArray = new int[]{100, 2, 3, 45, 33, 8, 4, 54};
swapEven(myArray);
System.out.println(Arrays.toString(myArray));
}
public static void swapEven(int[] array) {
if (array != null && array.length > 0) {
for (int i = 0; i < array.length / 2; i++) {
if (array[i] % 2 == 0 && array[array.length - 1 - i] % 2 == 0) {
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
}
} else {
System.out.println("Input array is empty or null.");
}
}
}
评论