【问题标题】:Java - Using the output of one method in the statement body of another method is not producing the expected resultJava - 在另一种方法的语句主体中使用一种方法的输出不会产生预期的结果
【发布时间】:2013-08-20 17:39:47
【问题描述】:

我有 2 个类,LotSelectionLotGen,在一个名为 lotterynumberselector 的包中。 LotSelection 有 2 种方法:LotPool()WinningSequence()LotPool() 旨在返回一个 ArrayList,其中包含从 0 到 49 的 50 个整数并对其进行打乱。 WinningSequence() 旨在创建一个 6 元素数组,其中包含 LotPool() 中生成的 ArrayList 中的前 6 个整数。

这是LotSelection的代码。

package lotterynumberselector;

import java.util.ArrayList;
import java.util.Collections;

public class LotSelection {

ArrayList<Integer> LotPool() {
    ArrayList<Integer> sequencedraw = new ArrayList<Integer>();
    for(int i = 0; i < 49; i++) {
          sequencedraw.add(i);
    }
    Collections.shuffle(sequencedraw);
    return sequencedraw;
}

int[] WinningSequence() {
    int[] WinningSequence = new int[6];
    int j = 0;
    while (j < 6) {
        WinningSequence[j] = LotPool().get(j);
        j++;
    }
    return WinningSequence;
}

}

LotGen 的目的是测试LotSelection 创建的输出是否完成了预期的任务。但是,WinningSequence() 的输出与 LotPool() 创建的前六个数字不匹配,我想知道为什么。我不确定是不是因为LotGenLotSelection 中的代码产生了意外的结果。我怀疑这是因为 LotPool() 正在生成一个 50 元素的 ArrayList,而 WinningSequence() 正在创建另一个 LotPool(),所以它是从不同的 50 元素 ArrayList 中创建的数组,但我不确定。

这是LotGen的代码:

package lotterynumberselector;

import java.util.ArrayList;
import java.util.Arrays;

public class LotGen {

public static void main(String [] args) {

    LotSelection a = new LotSelection();
    ArrayList<Integer> LotPool = new ArrayList<Integer>();
    LotPool = a.LotPool();
    System.out.println(LotPool);

    int[] WinSeq = new int[6];
    WinSeq = a.WinningSequence();
    System.out.println(Arrays.toString(WinSeq));

}

}

【问题讨论】:

  • 首先,尊重 java 命名约定并将方法的第一个字母小写。另外,给他们适当的访问修饰符。
  • 你能分享一下输出吗
  • 你调用了6次lotPool方法,可能是你想把它存储在一个变量中

标签: java list methods arraylist io


【解决方案1】:

在您的获胜序列方法中,您调用 LotPool() 方法。 LotPool 每次都会创建一个新的 ArrayList。

我会重构您的代码以在构造函数中初始化 50 个整数,并且不再这样做。使 LotPool() 成为返回数组列表的简单 getter 方法。

【讨论】:

    【解决方案2】:

    这样做的原因很简单:每次你为WinningSequence返回的列表选择一个新号码,你都会再次调用LotPool。您需要调用一次,将结果存储在一个变量中,并在每次循环中再次使用它。

    【讨论】:

      【解决方案3】:

      Collections.shuffle 的单参数版本将在您每次调用时产生不同的随机序列。为了获得可重复的结果,您需要传入自己的随机生成器,并适当处理播种。

      您可以这样做,但正如其他海报所指出的那样,这仍然非常低效。

      public class LotSelection {
      
      ArrayList<Integer> LotPool(long seed) {
          ArrayList<Integer> sequencedraw = new ArrayList<Integer>();
          for(int i = 0; i < 49; i++) {
                sequencedraw.add(i);
          }
          Collections.shuffle(sequencedraw, new Random(seed));
          return sequencedraw;
      }
      
      int[] WinningSequence(long seed) {
          int[] WinningSequence = new int[6];
          int j = 0;
          while (j < 6) {
              WinningSequence[j] = LotPool(seed).get(j);
              j++;
          }
          return WinningSequence;
      }
      
      }
      

      【讨论】:

        【解决方案4】:

        这是您的 WinningSequence 方法中的问题:

            WinningSequence[j] = LotPool().get(j);
        

        每次添加到数组时都会得到列表。这将导致仅获取列表的第一个元素。您只需在循环之外执行一次。

        以下是更新 WinningSequence 方法的方法:

        int[] WinningSequence() {
            int[] WinningSequence = new int[6];
            int j = 0;
            ArrayList<Integer> LotPool = LotPool().get(j)
            while (j < 6) {
                WinningSequence[j] = LotPool .get(j);
                j++;
            }
            return WinningSequence;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 1970-01-01
          • 2018-03-10
          • 1970-01-01
          • 2012-01-23
          • 1970-01-01
          相关资源
          最近更新 更多