用Java生成随机数数组

时间:2020-06-07 10:54:33

标签: java arrays

我正在编写一个代码,该代码应生成一个长度为100的数组,然后将数组中的数字分隔为:

  1. 数字是4的倍数。
  2. 不是4的倍数的数字。

下面是我的代码,但是,我得到了一个奇怪的输出(很多零)。如何确定问题所在?

public class Assignment8 {
    public static void main(String[] args) {
        // Defined array to hold the values
        int[] randomValueArray = new int[100];
        int[] mod4ValueArray = new int[100];
        int[] nonMod4ValueArray = new int[100];

        // Initiate the randomValue Array
        for (int i = 0; i < 100; ++i) {
            // Generate a random number from the range 1 to 100
            int randomValue = (int) (Math.random() * 100 + 1);
            randomValueArray[i] = randomValue;
        }

        // Pass the array to the class method to separate mod4
        // value and non mod4 value from the randomized array
        mod4ValueArray = isMod4(randomValueArray);
        nonMod4ValueArray = isNonMod4(randomValueArray);

        // Print out the two result arrays
        System.out.println("Randomly generated numbers that are multiples of four: ");
        for (int i = 0; i < mod4ValueArray.length; ++i) {
            System.out.println(mod4ValueArray[i]);
        }
        System.out.println("Randomly generated numbers that are not multiples of four: ");
        for (int i = 0; i < nonMod4ValueArray.length; ++i) {
            System.out.println(nonMod4ValueArray[i]);
        }
    }

    // Mod4 Checker Method
    public static int[] isMod4(int[] array) {
        int[] resultArray = new int[array.length];
        for (int i = 0; i < array.length; ++i) {
            int itemValue = array[i];
            if ((array[i] % 4) == 0) {
                resultArray[i] = itemValue;
            }
        }
        return resultArray;
    }

    // Non Mod 4 Checker Method
    public static int[] isNonMod4(int[] array) {
        int[] resultArray = new int[array.length];
        for (int i = 0; i < array.length; ++i) {
            int itemValue = array[i];
            if ((itemValue % 4) != 0) {
                resultArray[i] = itemValue;
            }
        }
        return resultArray;
    }
}

3 个答案:

答案 0 :(得分:0)

尝试一下。

int[] randomValueArray = new Random().ints(100, 1, 101).toArray();
int[] mod4ValueArray = IntStream.of(randomValueArray).filter(i -> i % 4 == 0).toArray();
int[] nonMod4ValueArray = IntStream.of(randomValueArray).filter(i -> i % 4 != 0).toArray();

答案 1 :(得分:0)

尝试一下:

new Random().ints(100, 1, 100).filter(n -> n % 4 == 0).toArray();
new Random().ints(100, 1, 100).filter(n -> n % 4 != 0).toArray();

答案 2 :(得分:-2)

Java 8流式解决方案:

Map<Boolean, int[]> map = new Random().ints(10, 1, 100)
    .mapToObj(i -> i)
    .collect(partitioningBy(i -> i % 4 == 0, collectingAndThen(toList(), list -> list.stream()
        .mapToInt(i -> i)
        .toArray()
    )));

这会从1到100提取100个随机整数,然后将所有int装到Integer中,然后根据数字是否可除以4进行分区。然后,得到的List<Integer>转换为int[]

但是,我认为普通的for循环更好。您遇到的唯一困难是,您不知道每个数组将达到多大。因此,最后,您需要调整数组的大小。

int[] fourMods = new int[100];
int[] nonFourMods = new int[100];
int fourModsCardinality = 0;
int nonFourModsCardinality = 0;

Random random = new Random();
for (int i = 0; i < 100; i++) {
    int num = random.nextInt(100) + 1;
    if (num % 4 == 0) {
        fourMods[fourModsCardinality] = num;
        fourModsCardinality++;
    }
    else {
        nonFourMods[fourModsCardinality] = num;
        nonFourModsCardinality++;
    }
}