While-Loop 不检查完整条件(跳过第一部分)
While-Loop not checking the full conditional (first part is skipped)
我是 Java 的新手,我正在尝试编写一种方法,将 return 作为数组中最长 运行 数字的起始索引。 (如果数组是 {2,3,5,5,5,2,2} 那么 '2' 应该被 returned 因为它是最长的相同数字串的开始)。
在我的代码中,我有一个 while 循环,只要数组中的下一个数字等于当前值,它就会继续 运行ning。 (将 运行 直到出现新号码)。
while((i < values.length) && (values[i] == values[i+1]))
{
currentRun++;
i++;
}
如果 运行 出现在数组末尾,这可能会导致索引越界错误。 {3,7...5,5,5} 因为它将继续循环并最终尝试检查不存在的索引的值。为了防止这种情况发生,我尝试添加到条件中并确保索引值必须小于数组的长度但是当我使用 java 可视化工具时它总是继续 运行无论条件的第一部分如何,都要执行 for 循环。
任何人都可以解释为什么会发生这种情况以及如何解决它吗?
(下面是完整的程序代码)
public class NumberCube
{
public static int getLongestRun(int[] values)
{
int longestIndex = -1;
int currentIndex = 0;
int longestRun = 1;
int currentRun = 0;
for(int i = 0; i < values.length - 1; i++)
{
if(values[i] == values[i+1])
{
currentIndex = i;
while((i < values.length) && (values[i] == values[i+1]))
{
currentRun++;
i++;
}
}
if (currentRun > longestRun)
{
longestRun = currentRun;
longestIndex = currentIndex;
}
currentRun = 1;
}//end for loop
return longestIndex;
}//end method
public static void main(String[] args){
int[] values = {3, 5, 6, 6, 3, 6, 4, 4, 4, 2, 6, 4, 1, 1, 1, 1};
int longestRunIdx = getLongestRun(values);
if(longestRunIdx != 12){
System.out.println("Your code does not return the correct index.");
if(longestRunIdx == 2 || longestRunIdx == 6)
System.out.println("It is returning the start index of a run, but that run is not the longest.");
System.out.println("Remember that your code must return the start index of the longest run of tosses.");
} else {
System.out.println("Looks like your code works well!");
}
}
}
确实会检查,查询不会阻止无效访问,因为数组是从零开始的。因此,例如,如果 array.length == 1
,则 array[0]
有效,但 array[1]
无效。
对于 i == 0
,您访问了两者,因此 i+1
的索引无效。
我是 Java 的新手,我正在尝试编写一种方法,将 return 作为数组中最长 运行 数字的起始索引。 (如果数组是 {2,3,5,5,5,2,2} 那么 '2' 应该被 returned 因为它是最长的相同数字串的开始)。
在我的代码中,我有一个 while 循环,只要数组中的下一个数字等于当前值,它就会继续 运行ning。 (将 运行 直到出现新号码)。
while((i < values.length) && (values[i] == values[i+1]))
{
currentRun++;
i++;
}
如果 运行 出现在数组末尾,这可能会导致索引越界错误。 {3,7...5,5,5} 因为它将继续循环并最终尝试检查不存在的索引的值。为了防止这种情况发生,我尝试添加到条件中并确保索引值必须小于数组的长度但是当我使用 java 可视化工具时它总是继续 运行无论条件的第一部分如何,都要执行 for 循环。
任何人都可以解释为什么会发生这种情况以及如何解决它吗?
(下面是完整的程序代码)
public class NumberCube
{
public static int getLongestRun(int[] values)
{
int longestIndex = -1;
int currentIndex = 0;
int longestRun = 1;
int currentRun = 0;
for(int i = 0; i < values.length - 1; i++)
{
if(values[i] == values[i+1])
{
currentIndex = i;
while((i < values.length) && (values[i] == values[i+1]))
{
currentRun++;
i++;
}
}
if (currentRun > longestRun)
{
longestRun = currentRun;
longestIndex = currentIndex;
}
currentRun = 1;
}//end for loop
return longestIndex;
}//end method
public static void main(String[] args){
int[] values = {3, 5, 6, 6, 3, 6, 4, 4, 4, 2, 6, 4, 1, 1, 1, 1};
int longestRunIdx = getLongestRun(values);
if(longestRunIdx != 12){
System.out.println("Your code does not return the correct index.");
if(longestRunIdx == 2 || longestRunIdx == 6)
System.out.println("It is returning the start index of a run, but that run is not the longest.");
System.out.println("Remember that your code must return the start index of the longest run of tosses.");
} else {
System.out.println("Looks like your code works well!");
}
}
}
确实会检查,查询不会阻止无效访问,因为数组是从零开始的。因此,例如,如果 array.length == 1
,则 array[0]
有效,但 array[1]
无效。
对于 i == 0
,您访问了两者,因此 i+1
的索引无效。