java - 是否可以用 int[] 替换 Integer[]?

标签 java integer int

我正在尝试稍微重写我的代码。是否可以在我的代码中使用 int[] 数组而不是 Integer[]

当我将所有 Integer[] 替换为 int[] 时,我卡在了部分替换中:

.toArray(Integer[]::new);

.toArray(int[]::new);
public class Main {

    private static final int MODULO = (int) (Math.pow(10, 9) + 7);

    private static int modulate(final long result) {
        return (int) (result % MODULO);
    }

    //private static

    private static Integer[] steps(final int smaller, final int larger) {
        final int lcm = lowestCommonMultiple(smaller, larger);
        final int max = lcm / smaller;
        final int min = lcm / larger;
        final Integer[] result = new Integer[max * 2];

        int pos = 0;
        for (int i = 1; i <= max; i++) {
            result[pos++] = (i * smaller);
            if (i <= min) {
                result[pos++] = (i * larger);
            }
        }
        return Arrays.stream(result)
                .filter(Objects::nonNull)
                .sorted()
                .distinct()
                .toArray(Integer[]::new);
    }

    private static long nthNonZeroMagicalNumber(final int N, final int smaller, final int larger) {
        final Integer[] stepsInCycle = steps(smaller, larger);
        final long lcm = stepsInCycle[stepsInCycle.length - 1];
        final int inOneCycle = stepsInCycle.length;
        final int fullCycleCount = N / inOneCycle;
        int count = fullCycleCount * inOneCycle;
        final long evaluated = fullCycleCount * lcm;
        if (count == N) {
            return evaluated;
        }
        final int remainder = N - count - 1;
        return stepsInCycle[remainder] + evaluated;
    }

    private static int greatestCommonDenominator(int a, int b) {
        while (b > 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    public static int lowestCommonMultiple(final int a, final int b) {
        return a * (b / greatestCommonDenominator(a, b));
    }

    public static int nthMagicalNumber(final int N, final int A, final int B) {
        if (N == 0) {
            return 0;
        } else if (A == B) {
            final long result = (long) A * (long) N;
            return modulate(result);
        } else if (N == 1) {
            return modulate(Math.min(A, B));
        }
        return modulate(nthNonZeroMagicalNumber(N, Math.min(A, B), Math.max(A, B)));
    }

    public static void main(String[] args) {
        int result = nthMagicalNumber(53776, 22434, 31343);

    }
}

如果有人能给我任何建议,我将不胜感激。提前致谢!

最佳答案

假设您从 Integer[] 开始,您可以取消装箱流中的整数,这将给出 IntStream来自你的 Stream<Integer> .

// if result is an Integer[]
return Arrays.stream(result)
        .filter(Objects::nonNull)
        .sorted()
        .distinct()
        .mapToInt(n -> n) // this gives an IntStream
        .toArray();       // this returns an int[]

如果你真的改变了你的result要键入的数组 int[]那么您将不需要拆箱,但您将无法使用 nonNull 过滤掉元素不再是:数组中未写入的元素将为零而不是 null。所以你会:

// if result is an int[]
return Arrays.stream(result)
             .filter(n -> n!=0)
             .sorted()
             .distinct()
             .toArray();

使用列表而不是数组可能会更好。列表可以根据需要增加大小,因此您不必跟踪已到达的位置,或排除未写入的元素。

关于java - 是否可以用 int[] 替换 Integer[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56393301/

相关文章:

C++ 将字符串解析为具有特定类的整数

sql - 在 SQL Server 2012 中将专辑轨道编号作为 INT 插入表时出现语法错误

java - gradle 构建时出现 StackOverflowError

java - 当无法注入(inject) injects 子句中的类时,为什么 dagger 在编译时不会失败?

java - DNS 名称的解析,其中包括单个数字(例如 x.x.x.1)

Python 速度违规计算器返回 TypeError 错误

java - 如何向右旋转数组

c - 从 float 到整数

c++ - generic int -> enum conversion with templates 可能吗?

java - 当 JDBC 数据库编号在 ResultSet.getInt 期间超过 Java 的 int 大小时会发生什么?