【问题标题】:How do I create a method that meet this condition and return them? [duplicate]如何创建满足此条件的方法并返回它们? [复制]
【发布时间】:2021-12-08 13:38:18
【问题描述】:

如何创建一个具有大小为 x 的数组的方法,用 y 和 z 之间的随机数填充它,然后返回该数组?当我调用该方法时,我将拥有一定大小的数组(x),然后我将拥有 z 和 y 的范围。

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

    使用Random.ints(streamSize, origin, bound) 创建一个随机值IntStream,这些值被收集到数组中:

    public static int[] buildArray(int size, int from, int to) {
        return new Random() // or ThreadLocalRandom.current()
                .ints(size, from, to) // IntStream
                .toArray();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 2020-08-20
      • 2018-03-05
      • 2018-10-23
      • 2021-03-29
      • 1970-01-01
      相关资源
      最近更新 更多