如何在适当的位置反转数组(不创建新数组)?
public static int[] reverseArrayWithoutTempArray(int[] array) {
int i = 0, j = array.length - 1;
for (i = 0; i < array.length / 2; i++, j--) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
Array.prototype.reverse = function() {
for(var i = 0, j = this.length-1; i < j; i++, j--) {
var tmp = this[i];
this[i] = this[j];
this[j] = tmp;
}
return this;
};
家庭作业只是来自我的伪代码,你通过不指定你想要的语言而相对容易:-)
把它变成你选择的语言:
Set i1 to index of first element in array
Set i2 to index of last element in array
while i1 < i2:
Set temporary variable to element number i1
Set element number i1 to element number i2
Set element number i2 to temporary value
Add 1 to i1
Subtract 1 from i2
理想的做法是在头脑中实际运行该算法,使用一张纸来跟踪变量:
i1
和i2
。temporary variable
。我倾向于为更简单的算法做到这一点。更难的我插入调试语句,以便计算机可以为我做那些笨拙的工作。从一张纸开始,这样:
i1 | i2 | tempvar | el[0] | el[1] | el[2] | el[3] | el[4] | el[5]
---+----+---------+-------+-------+-------+-------+-------+------
H e l l o !
然后逐个执行这些步骤,检查和/或更改每个列。这将使您了解它的工作方式远比给出一些代码更好。
在不使用java创建新数组的情况下反转字符数组。
import java.util.*;
//Reverse string array
public static void reverseArray(String[] array){
int middle = array.length / 2;
String temp;
int j = array.length -1;
for (int i = 0 ; i < middle; i++) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
j--;
}
System.out.println(Arrays.toString(array));
}
如果要反转int数组,则必须将public static void reverseArray(String[] array)
更改为public static void reverseArray(int[] array)
,将String temp
更改为int temp
。
例:
public static void main (String[] args) throws java.lang.Exception{
String[] array = {"Smith", "Peter", "Michel", "John"};
reverseArray(array);
}
输出:
[John, Michel, Peter, Smith]
使用单个变量作为临时缓冲区,不断交换端点。在伪代码中:
temp = a[0]
a[0] = a[size - 1]
a[size - 1] = temp
等等。
public static void main(String args[]){
int j=arr.length;
for(int i=0;i<arr.length/2;i++){
int temp=arr[i];
arr[i]=arr[j-1-i];
arr[j-1-i]=temp;}
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
不要在内存中反转数组,只需向后迭代它!
这是在不使用临时变量或其他数组的情况下反转数组元素的解决方案。这只适用于Java 8及更高版本。
void invertUsingStreams(Object[] arr2) {
IntStream.rangeClosed(1,arr2.length)
.mapToObj(i -> arr2[arr2.length-i])
.forEach(System.out::println);
}
谢谢,
这是一个完整的程序,只需复制粘贴并在IDE中运行它:
public class ReverseArrayWithoutAnotherArray {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int middle = array.length / 2;
int temp;
int j = array.length -1;
for(int a : array){
System.out.println(" before reverse :: " + a);
}
for (int i = 0 ; i < middle; i++, j--) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
for(int a : array){
System.out.println(" after reverse :: " + a);
}
}
}
输出:
before reverse :: 1
before reverse :: 2
before reverse :: 3
before reverse :: 4
before reverse :: 5
before reverse :: 6
before reverse :: 7
before reverse :: 8
before reverse :: 9
before reverse :: 10
after reverse :: 10
after reverse :: 9
after reverse :: 8
after reverse :: 7
after reverse :: 6
after reverse :: 5
after reverse :: 4
after reverse :: 3
after reverse :: 2
after reverse :: 1
在不创建新数组的情况下反转字符数组
public static void main(String[] args) {
char[] a = {'1', '2', '3','4'};
char temp = ' ';
for (int i = 0; i < a.length / 2; i++) {
temp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = temp;
}
System.out.println(a);
}