java数组中的轨道数字
可能的重复:
跟踪数组中的最小数字 < /p>
我正在尝试跟踪数组中的最小数字。它在部分时间有效,我只是不明白为什么它这样做
for (int i = 0; i < candidate.length; i++) {
for (int j = 1; j < candidate.length; j++) {
if (candidate[j] < candidate[i]) {
System.out.println(candidate[j]+ " "+candidate[i]);
if(!tracking.contains(dictionary.get(j))) {
tracking.add(dictionary.get(j));
System.out.println(dictionary.get(j));
writeOut.add(space+dictionary.get(j)+" removed");
}
min[i] = j;
}
}
}
它适用于这样的数字,
2
2
1
2
1
但不适用于这样的数字:
3
3
2
4
4
对于这个,当显然最小的数字是 2 时,它会得到 2 和 3!
Possible Duplicate:
Keep track of the lowest numbers in an array
I am trying to keep track of the lowest numbers in an array. And it works PART of the time and I just can't figure it out why its doing this
for (int i = 0; i < candidate.length; i++) {
for (int j = 1; j < candidate.length; j++) {
if (candidate[j] < candidate[i]) {
System.out.println(candidate[j]+ " "+candidate[i]);
if(!tracking.contains(dictionary.get(j))) {
tracking.add(dictionary.get(j));
System.out.println(dictionary.get(j));
writeOut.add(space+dictionary.get(j)+" removed");
}
min[i] = j;
}
}
}
And it works for numbers like
2
2
1
2
1
but not for numbers like :
3
3
2
4
4
for this it gets 2 and three when clearly the lowest number is 2!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不直接将数字放入 a 中
并使用 检索最小数字
但是,由于您所在管辖区的严厉起诉,您必须使用数组来执行此操作,因此您可以这样做:
Why don't you just put your numbers in a
And retrieve the lowest number with
But since you have to do it with an array because of severe prosecution in your jurisdiction, this is how you can do it:
使用这个代替你已经使用过的:
use this instead of what you have used:
以下方法将以列表形式返回整数数组中
第一个最小值的所有位置。因此,它将返回一个包含输入{4,3,1,1,5}
值2
和3
的列表,保持简单 -稍后尝试通过查看 Math.min 或增强的 for 循环来改进它(如果您对最小值感兴趣而不是它在数组中的位置)
The following method will return
the firstall positions of the minimum value in an integer array as a list. So it will return a list with values2
and3
for the input{4,3,1,1,5}
Kept it simple - try to improve it later by looking at
Math.min
or the enhanced for loop (if you're interested in the lowest value rather then it's position in the array)