java数组中的轨道数字

发布于 2024-11-14 01:12:55 字数 916 浏览 1 评论 0原文

可能的重复:
跟踪数组中的最小数字 < /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨曦÷微暖 2024-11-21 01:12:55

为什么不直接将数字放入 a 中

TreeSet<Integer> set = ...

并使用 检索最小数字

Integer lowest = set.first();

但是,由于您所在管辖区的严厉起诉,您必须使用数组来执行此操作,因此您可以这样做:

int[] array = ...

// If your array is empty, then you will get MAX_VALUE as a result.
int lowest = Integer.MAX_VALUE;
for (int i : array) {
    lowest = Math.min(i, lowest);
}

Why don't you just put your numbers in a

TreeSet<Integer> set = ...

And retrieve the lowest number with

Integer lowest = set.first();

But since you have to do it with an array because of severe prosecution in your jurisdiction, this is how you can do it:

int[] array = ...

// If your array is empty, then you will get MAX_VALUE as a result.
int lowest = Integer.MAX_VALUE;
for (int i : array) {
    lowest = Math.min(i, lowest);
}
鹤舞 2024-11-21 01:12:55
for (int i = 0; i < candidate.length; i++) {
  for (int j = 0; j < candidate.length; j++) {

使用这个代替你已经使用过的:

for (int i = 0; i < candidate.length; i++) {
  for (int j = 1; j < candidate.length; j++) {
for (int i = 0; i < candidate.length; i++) {
  for (int j = 0; j < candidate.length; j++) {

use this instead of what you have used:

for (int i = 0; i < candidate.length; i++) {
  for (int j = 1; j < candidate.length; j++) {
情魔剑神 2024-11-21 01:12:55

以下方法将以列表形式返回整数数组中第一个最小值的所有位置。因此,它将返回一个包含输入 {4,3,1,1,5}23 的列表,

public static List<Integer> findPositionsOfLowestNumber(int[] values) {
   List<Integer> result = new ArrayList();   // start with an empty list
   int min = Integer.MAX_VALUE;   // initial min value
   for (int i = 0; i < values.length; i++) {
     if (values[i] < min) {
       min = values[i];
       result.clear();
       result.add(i);
     } else if (values[i] == min) {
       result.add(i);
     }
   }
   return result;
}

保持简单 -稍后尝试通过查看 Math.min 或增强的 for 循环来改进它(如果您对最小值感兴趣而不是它在数组中的位置)

The following method will return the first all positions of the minimum value in an integer array as a list. So it will return a list with values 2 and 3 for the input {4,3,1,1,5}

public static List<Integer> findPositionsOfLowestNumber(int[] values) {
   List<Integer> result = new ArrayList();   // start with an empty list
   int min = Integer.MAX_VALUE;   // initial min value
   for (int i = 0; i < values.length; i++) {
     if (values[i] < min) {
       min = values[i];
       result.clear();
       result.add(i);
     } else if (values[i] == min) {
       result.add(i);
     }
   }
   return result;
}

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文