[英]This method is not inserting the objects into the ArrayList. Why?
public void addClimb(String peakName, int climbTime){
for(int i = 0; i < climbList.size()-1; i++){
if(peakName.substring(0,1).compareTo(climbList.get(i).getName().substring(0,1)) <= 0){
climbList.add(i, new ClimbInfo(peakName, climbTime));
} else {
climbList.add(new ClimbInfo(peakName, climbTime));
}
}
}
The Goal is to have it take in an peakName and climbTime, have it loop through the climbTime objects in climbList, check for when the first letter of the parameter comes before the first letter of the climbTime in the loop, and places it as soon as that happens, but im getting an out of bounds error when i enter in several ClimbInfos and try to print them. 目标是让它接受peakName和climbTime,使其在climbList中的climbTime对象之间循环,检查参数的第一个字母何时在循环中的climbTime的第一个字母之前,并尽快将其放置发生这种情况,但是当我输入几个ClimbInfos并尝试打印它们时,我遇到了界外错误。 This method is not properly inserting the ClimbInfo into climbTime properly. 此方法未正确将ClimbInfo正确插入climbTime。
Could someone explain what I'm doing wrong? 有人可以解释我在做什么错吗?
Depending on initial conditions, your function will do one of two things: 根据初始条件,您的函数将执行以下两项操作之一:
climbList
is initially empty, or initially contains only one element, then the test i < climbList.size() - 1
will fail, so the function will return immediately, doing nothing. 如果climbList
最初为空,或者最初仅包含一个元素,则测试i < climbList.size() - 1
将失败,因此该函数将立即返回,不执行任何操作。 climbList
initially contains more than one element, then the test i < climbList.size() - 1
will always succeed, because every pass through the loop will add an element to climbList
and will increment i
by one. 如果climbList
最初包含多个元素,则测试i < climbList.size() - 1
将始终成功,因为每次通过循环都会在climbList
添加一个元素并将i
加1。 So i < climbList.size() - 1
is true before the iteration, then it will be true after the iteration. 所以i < climbList.size() - 1
在迭代之前为true,然后在迭代之后为true。 So you have an infinite loop. 所以你有一个无限循环。 I don't think that either of these is what you want. 我认为这些都不是您想要的。
Basically you are looping through the whole list, adding a record each time through the loop. 基本上,您遍历整个列表,每次遍历都添加一条记录。 You probably want something like: 您可能想要类似的东西:
public void addClimb(String peakName, int climbTime){
for(int i = 0; i < climbList.size(); i++){
if(peakName.substring(0,1).compareTo(climbList.get(i).getName().substring(0,1)) >= 0){
climbList.add(i, new ClimbInfo(peakName, climbTime));
return;
}
}
climbList.add(new ClimbInfo(peakName, climbTime));
}
You failed to explain what the problem is. 您无法解释问题所在。
This method is not properly inserting the ClimbInfo into climbTime properly. 此方法未正确将ClimbInfo正确插入climbTime。
Doesn't tell me what happens. 不要告诉我会发生什么。 Runtime exception? 运行时异常? Compile error? 编译错误? Or are you left with an empty collection at the end? 还是最后剩下一个空的收藏夹?
If you receive an exception, please include it along with a stack trace. 如果收到异常,请将其与堆栈跟踪一起包括在内。
If we assume you have an empty collection, that can only mean one thing: the for loop condition is never reached. 如果我们假设您有一个空集合,那只能意味着一件事:永远不会达到for循环条件。 I can tell this for certain because the if statement has an else which inserts. 我可以肯定地说出来,因为if语句有一个else插入。 Therefore, every iteration of the loop is guaranteed to insert. 因此,保证循环的每次迭代都会插入。 Thus, the loop must not be iterating. 因此,循环一定不能迭代。
I suspect it's the size checking. 我怀疑是尺寸检查。
for(int i = 0; i < climbList.size()-1; i++){
That logic is almost certainly wrong for two reasons: 该逻辑几乎肯定是错误的,原因有两个:
you should not modify the list while iterating it. 您不应在迭代时修改列表。
I assume that what you are trying to achieve here is custom sorting. 我假设您要在此处实现的是自定义排序。 For that you must implement Comparable interface in ClimbInfo
and use Collections.sort(climbList)
. 为此,您必须在ClimbInfo
实现Comparable接口,并使用Collections.sort(climbList)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.