比较数组时出现 ArrayIndexOutOfBoundsException
ArrayIndexOutOfBoundsException when comparing arrays
每当我尝试 运行 这种方法来比较数组时,我总是得到 ArrayIndexOutOfBoundsException。我做错了什么?
public static boolean areIdentical(int[] m, int[] n) {
//write a loop that compares each individual element of the two arrays.
//if a mismatch is found, return false
boolean identical = false;
for(int x = 0; m.length > x || n.length > x; x++) {
if(m[x] == n[x]) {
identical = true;
} else {
identical = false;
}
}
return identical;
}
问题是,您使用的是析取 OR。这意味着只要您的索引在至少一个数组的范围内,它就会尝试访问它。
您应该将您的条件更改为:
m.length > x && n.length > x
或者,更好的是,事先检查它们的长度是否相同。如果它们不是,则它们不可能相同。但是,我不知道这里的完整程序要求,因此这可能不是预期的行为。
if(m.length != n.length){
return false;
}
boolean identical = false;
//...
你可以做一件事,先计算小数组的长度。
public static boolean areIdentical(int[] m, int[] n)
{
int length;
if(m.length<n.length){
length=m.length
}else{
length=n.length;
}
boolean identical = false;
for(int x = 0; x<length; x++){
if(m[x] == n[x]){
identical = true;
break;
}
else
{
identical = false;
}
}
//write a loop that compares each individual element of the two arrays.
//if a mismatch is found, return false
return identical;
}
早期计算长度的好处是您不需要执行 && 或 ||对于每个 iteration.And 当你发现它们相同时使用 break 它将保存你的迭代。
但是正如您的评论所说,您需要检查两个数组的单个元素,我猜您需要使用 2 个 for 循环:-
boolean identical = false;
for(int x = 0; x<n.length; x++){
for(int y=0;y<m.length;y++){
if(m[y] == n[x]){
identical = true;
break;
}
}
}
return identical;
首先尝试比较两个数组的.length,看看它们是否相等。如果不是,则它们不相同。
如果它们的长度相同,请尝试将您的 for 循环更改为这样的内容
boolean identical = true;
if(m.length() != n.length()) identical = false;
if(identical)
{
for(int x = 0; x < m.length(); x++)
{
if(m[x] != n[x])
{
identical = false;
}
}
}
return identical;
这段代码应该可以工作,因为您似乎只是在寻找任何不匹配的部分,而不是全部
每当我尝试 运行 这种方法来比较数组时,我总是得到 ArrayIndexOutOfBoundsException。我做错了什么?
public static boolean areIdentical(int[] m, int[] n) {
//write a loop that compares each individual element of the two arrays.
//if a mismatch is found, return false
boolean identical = false;
for(int x = 0; m.length > x || n.length > x; x++) {
if(m[x] == n[x]) {
identical = true;
} else {
identical = false;
}
}
return identical;
}
问题是,您使用的是析取 OR。这意味着只要您的索引在至少一个数组的范围内,它就会尝试访问它。
您应该将您的条件更改为:
m.length > x && n.length > x
或者,更好的是,事先检查它们的长度是否相同。如果它们不是,则它们不可能相同。但是,我不知道这里的完整程序要求,因此这可能不是预期的行为。
if(m.length != n.length){
return false;
}
boolean identical = false;
//...
你可以做一件事,先计算小数组的长度。
public static boolean areIdentical(int[] m, int[] n)
{
int length;
if(m.length<n.length){
length=m.length
}else{
length=n.length;
}
boolean identical = false;
for(int x = 0; x<length; x++){
if(m[x] == n[x]){
identical = true;
break;
}
else
{
identical = false;
}
}
//write a loop that compares each individual element of the two arrays.
//if a mismatch is found, return false
return identical;
}
早期计算长度的好处是您不需要执行 && 或 ||对于每个 iteration.And 当你发现它们相同时使用 break 它将保存你的迭代。
但是正如您的评论所说,您需要检查两个数组的单个元素,我猜您需要使用 2 个 for 循环:-
boolean identical = false;
for(int x = 0; x<n.length; x++){
for(int y=0;y<m.length;y++){
if(m[y] == n[x]){
identical = true;
break;
}
}
}
return identical;
首先尝试比较两个数组的.length,看看它们是否相等。如果不是,则它们不相同。
如果它们的长度相同,请尝试将您的 for 循环更改为这样的内容
boolean identical = true;
if(m.length() != n.length()) identical = false;
if(identical)
{
for(int x = 0; x < m.length(); x++)
{
if(m[x] != n[x])
{
identical = false;
}
}
}
return identical;
这段代码应该可以工作,因为您似乎只是在寻找任何不匹配的部分,而不是全部