我是java编程的初学者。我确实理解如何打印反转阵列,但不知道如何打印一半。例如:
原始数组A = {1,2,3,4,5,6,7,8,9,0}
功能后:
转换数组A'= {1,2,3,4,5,0,9,8,7,6}
答案 0 :(得分:2)
private void reverse(int[] ar, int i, int j) {
if(i>j)
return;
else{
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
reverse(ar, ++i, --j);
}
}
从主方法中调用reverse(ar, (ar.length/2), ar.length-1)
。
答案 1 :(得分:0)
尝试以下步骤
答案 2 :(得分:0)
请找到理想的解决方案人员!
导入阵列和扫描仪软件包
public static void main(String[] args) {
Scanner size = new Scanner(System.in);
System.out.println("Enter Array Size : ");
int arraySize = size.nextInt();
Scanner data = new Scanner(System.in);
System.out.println("Enter Array Data : ");
int arrayData[] = new int[arraySize];
for(int inputLoop = 0; inputLoop < arraySize; inputLoop++) {
arrayData[inputLoop] = data.nextInt();
}
System.out.println("Original Array : " + Arrays.toString(arrayData));
int centerPoint = arrayData.length / 2;
int endPoint = arrayData.length - 1;
reverse(arrayData, centerPoint, endPoint);
System.out.println("Transformed Array : " + Arrays.toString(arrayData));
data.close();
size.close();
}
private static void reverse(int[] arrayData, int centerPoint, int endPoint) {
if(centerPoint > endPoint || centerPoint == endPoint) {
return;
}
else {
arrayData[centerPoint] = arrayData[centerPoint] - arrayData[endPoint];
arrayData[endPoint] = arrayData[centerPoint] + arrayData[endPoint];
arrayData[centerPoint] = arrayData[endPoint] - arrayData[centerPoint];
reverse(arrayData, ++centerPoint, --endPoint);
}
}
}