gpt4 book ai didi

对整数数组进行排序时出现 java.lang.ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 04:59:33 26 4
gpt4 key购买 nike

<分区>

场景:

我需要从标准输入流中读取一个整数数组,以这样一种方式对其进行重新排序,即素数后面始终跟有两个偶数,以确保按照以下顺序从数组中提取素数和偶数它们用于构建 prime-2even 集。任何不能成为集合一部分的剩余数字都可以按照它们出现的顺序放在数组的末尾。输入(多行)和预期输出(多行)如下:

输入:

8 5 9 7 8 5 4 6 8

预期输出:

5 8 4 7 6 8 9 5

尝试:

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int totalElements = Integer.parseInt(scanner.nextLine());

Integer[] integers = new Integer[totalElements];
Integer[] primeTwoEvens = new Integer[totalElements];
Integer[] leftOvers = new Integer[totalElements];

for (int i = 0; i < integers.length; i++) {
integers[i] = scanner.nextInt();
}

// LOGIC
int notNullCounter = 0; // Variable to track how many elements of integers array has been set to null.
while (notNullCounter != integers.length) { // Repeat this process until all the elements of the integers array are null.
for (int i = 0; i < integers.length; i++) { // Iterate the integers array and check for primeTwoEvens and populate the primeTwoEvens array.
if (integers[i] != null) { // Is the element of integers array to be processed null? If no, proceed.
if (isPrime(integers[i])) { // Is the element of integers array prime? If yes, proceed.
System.out.println(integers[i] + " is prime..."); // Print statement for debugging purpose.
primeTwoEvens[i] = integers[i]; // Since the element of integers array is prime, add it to the primeTwoEvens array.
integers[i] = null; // Set this index of integers array to null.
notNullCounter++; // As one element of integers array has been set to null, increment the null counter.


int evenCounter = 0; // Variable to track even number occurrences.
while (evenCounter != 2) { // Repeat this process until 2 even numbers are not found.
for (int j = ++i; j <= integers.length; j++) { // Iterate the remaining elements of integers array and check for next two even numbers.
if (isEven(integers[j])) { // Is the element of integers array even? If yes, proceed.
System.out.println(integers[j] + " is even..."); // Print statement for debugging purpose.
evenCounter++; // Since the element of integers array is even, increment the even counter.
primeTwoEvens[++i] = integers[j]; // Since the element of integers array is even, add it to the primeTwoEvens array as well.
integers[j] = null; // Set this index of integers array to null.
notNullCounter++; // As one element of integers array has been set to null, increment the null counter.
}
}
}
} /*else { // Element is not prime.

}*/
}
}
//break;
}// End of while

/*System.out.println("@@@@@@@@@@@@ PRINTING THE FINAL SORTED ARRAY @@@@@@@@@@@@");
for (Integer integer : integers) {
System.out.println(integer);
}*/
}

抛出一个 java.lang.ArrayIndexOutOfBoundsException

输出:

5 is prime...
8 is even...
4 is even...
6 is even...
8 is even...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at Ideone.main(Ideone.java:125)

第 125 行:

if (isEven(integers[j]))

注意:我只能使用 Java 标准 API 和 JDK 7。

我如何解决问题和最终任务?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com