如何撤消此代码?我以为我正在使用正确的方法来反转这个数组,但它仍然不起作用......
我希望有人可以帮助我。 tq
所以,这是我的代码:
import java.util.Scanner ;
public class ArrayYear
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int[] arr = new int[3];
int[] year = new int[3];
//read element into array
for ( int i=0 ; i <= arr.length ; i++ )
{
arr[i] = in.nextInt() ; // input value
for ( int j=0 ; j<=arr.length/2 ; j++ ){
int temp = arr[j];
arr[j] = arr[arr.length - i - 1 ];
arr[arr.length - i - 1 ] = temp;
}
year[i]= in.nextInt();
System.out.println( "ID :" + arr[i] + "("+ year[i] +")" ) ;
}
}
}
注意:需要反转的数组只是第一个数组arr[i]
答案 0 :(得分:1)
你必须首先填充数组然后反转它,并且循环应该是10-25 14:25:04.083 3438-3438/? I/art: Late-enabling -Xcheck:jni
10-25 14:25:04.254 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid D/AndroidRuntime: Shutting down VM
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: FATAL EXCEPTION: main
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: Process: com.appdynamics.pmdemoapps.android.ECommerceAndroid, PID: 3438
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: java.lang.RuntimeException: Unable to create application com.appdynamics.pmdemoapps.android.ECommerceAndroid.CustomApplication: java.lang.IllegalArgumentException: AppKey cannot be null or empty
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4641)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.ActivityThread.access$1500(ActivityThread.java:155)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5343)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: AppKey cannot be null or empty
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.appdynamics.eumagent.runtime.Instrumentation.a(Instrumentation.java:585)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.appdynamics.eumagent.runtime.Instrumentation.a(Instrumentation.java:291)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.appdynamics.eumagent.runtime.Instrumentation.start(Instrumentation.java:245)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.appdynamics.eumagent.runtime.Instrumentation.start(Instrumentation.java:229)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.appdynamics.pmdemoapps.android.ECommerceAndroid.CustomApplication.onCreate(CustomApplication.java:39)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1035)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4638)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.ActivityThread.access$1500(ActivityThread.java:155)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5343)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
10-25 14:25:04.264 3438-3438/com.appdynamics.pmdemoapps.android.ECommerceAndroid E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
10-25 14:25:06.342 3438-3438/? I/Process: Sending signal. PID: 3438 SIG: 9
。反转数组代码将是
i<=arr.length-1
输入
public static int[] reverseArray(int a[]) {
int b[] = new int[a.length];
int index = 0;
for (int i = a.length - 1; i >= 0; i--)
b[index++] = a[i];
return b;
}
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System. in );
int c[] = new int[5];
int index = 0;
while (sc.hasNextLine()) {
c[index++] = Integer.parseInt(sc.nextLine().trim());
}
System.out.println("Before Reverse " + Arrays.toString(c));
System.out.println("After Reverse " + Arrays.toString(reverseArray(c)));
}
输出
1
2
3
4
5
答案 1 :(得分:0)
import java.util.Collections;
import java.util.Scanner;
public class ArrayYear {
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
int[] arr = new int[3];
int[] year = new int[3];
// read element into array
for (int i = 0; i < arr.length; i++) {
// input value
arr[i] = in .nextInt();
year[i] = in .nextInt();
// reverse array
for (int j = 0; j < arr.length / 2; j++) {
int temp = arr[j];
arr[j] = arr[arr.length - j - 1];
arr[arr.length - j - 1] = temp;
}
for (int j = 0; j < year.length / 2; j++) {
int temp = year[j];
year[j] = year[year.length - j - 1];
year[year.length - j - 1] = temp;
}
}
for (int i = 0; i < arr.length; i++) {
// print array
System.out.println("ID :" + arr[i] + "(" + year[i] + ")");
}
}
}