谁能告诉我为什么我的代码会出现 java.lang.ArrayIndexOutOfBoundsException 错误?
Can someone tell me why am I getting java.lang.ArrayIndexOutOfBoundsException error in my code?
我正在开发一个 Java 程序来遍历两个数组并比较第一个和第二个是否匹配。它应该 return 所有不匹配的 numbers/strings 作为数组列表。我完成了,但我不确定为什么会收到 ArrayIndexOutOfBoundsException
error。这是我的代码:
package test;
import java.awt.List;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class ArrayComparer {
public static ArrayList<String> ArrayComparer(String[] arrayOne, String[] arrayTwo){
// if one is bigger than start by comparing the smaller one to the bigger one
// as if it were the other way the bigger one would run out over numbers to compare
// declaring the array for holding all the non-matching telephone numbers to be returned
ArrayList<String> nonMatchingTelephoneNumbers = new ArrayList<String>();
for(int i = 0; i<arrayOne.length; i++){
int strikes = 0;
// for each value of the first one it should go through all the values of the second and compare each
for(int i2 = 0; i<arrayTwo.length; i2++){
if(arrayOne[i] != arrayTwo[i2]){
strikes++;
if(strikes == arrayTwo.length){
// meaning it has gone through ALL of arrayTwo and couldn't find a match
nonMatchingTelephoneNumbers.add(arrayOne[i]);
}
}
}
}
return nonMatchingTelephoneNumbers;
}
public static void main(String[] args) throws IOException {
// declaring the first list of telephone number
String[] ArrayListOne;
// declaring the second list of telephone numbers
String[] ArrayListTwo;
// splitting up the user input of telephone numbers by commas
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter your first array of telephone numbers, split by commas");
ArrayListOne = myObj.nextLine().split(","); // Read user input
// once it has iterated through all of the telephone numbers in the first list, ask for the second list
Scanner myObj2 = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter your second array of telephone numbers, split by commas");
ArrayListTwo = myObj2.nextLine().split(","); // Read the second user input
// once it has collected and sorted all the user input, the ArrayCOmparer method should be called
// to compare them and return the telephone numbers that DON'T MATCH
ArrayComparer(ArrayListOne, ArrayListTwo);
}
}
错误出现在 第 22 行,它说 if(arrayOne[i] != arrayTwo[i2]){
。它也没有说第 22 行错误直到我运行为止。谁能告诉我为什么我会收到这个:console error?
嗨,我认为这是导致错误的原因
for(int i2 = 0; i<arrayTwo.length; i2++){
您应该将其替换为:
for(int i2 = 0; i2<arrayTwo.length; i2++){
我正在开发一个 Java 程序来遍历两个数组并比较第一个和第二个是否匹配。它应该 return 所有不匹配的 numbers/strings 作为数组列表。我完成了,但我不确定为什么会收到 ArrayIndexOutOfBoundsException
error。这是我的代码:
package test;
import java.awt.List;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class ArrayComparer {
public static ArrayList<String> ArrayComparer(String[] arrayOne, String[] arrayTwo){
// if one is bigger than start by comparing the smaller one to the bigger one
// as if it were the other way the bigger one would run out over numbers to compare
// declaring the array for holding all the non-matching telephone numbers to be returned
ArrayList<String> nonMatchingTelephoneNumbers = new ArrayList<String>();
for(int i = 0; i<arrayOne.length; i++){
int strikes = 0;
// for each value of the first one it should go through all the values of the second and compare each
for(int i2 = 0; i<arrayTwo.length; i2++){
if(arrayOne[i] != arrayTwo[i2]){
strikes++;
if(strikes == arrayTwo.length){
// meaning it has gone through ALL of arrayTwo and couldn't find a match
nonMatchingTelephoneNumbers.add(arrayOne[i]);
}
}
}
}
return nonMatchingTelephoneNumbers;
}
public static void main(String[] args) throws IOException {
// declaring the first list of telephone number
String[] ArrayListOne;
// declaring the second list of telephone numbers
String[] ArrayListTwo;
// splitting up the user input of telephone numbers by commas
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter your first array of telephone numbers, split by commas");
ArrayListOne = myObj.nextLine().split(","); // Read user input
// once it has iterated through all of the telephone numbers in the first list, ask for the second list
Scanner myObj2 = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter your second array of telephone numbers, split by commas");
ArrayListTwo = myObj2.nextLine().split(","); // Read the second user input
// once it has collected and sorted all the user input, the ArrayCOmparer method should be called
// to compare them and return the telephone numbers that DON'T MATCH
ArrayComparer(ArrayListOne, ArrayListTwo);
}
}
错误出现在 第 22 行,它说 if(arrayOne[i] != arrayTwo[i2]){
。它也没有说第 22 行错误直到我运行为止。谁能告诉我为什么我会收到这个:console error?
嗨,我认为这是导致错误的原因
for(int i2 = 0; i<arrayTwo.length; i2++){
您应该将其替换为:
for(int i2 = 0; i2<arrayTwo.length; i2++){