繁体   English   中英

为什么不能将list.size设置为整数数组的长度?

[英]Why can't I set list.size as the length of my integer array?

我一直在努力消除我不了解的错误。 在下面的代码的第190行(int s = n [0])中,我得到了数组超出范围的异常,这与我缩小范围与数组e有关。 数组e在下面给出的代码的顶部声明,带有对象列表a的长度/大小(a.size()),如果我用任意整数(例如10)替换size,错误消失。

为什么不能将a.size设置为数组的长度? 有办法解决这个问题吗?

代码摘要:powerize(int n)应该写一个数字n作为具有最大指数的幂。 factorize将数字分解为素数的乘积,而gcd返回整数数组的最大公约数。

public static Power powerize(int n) throws IllegalArgumentException {

    List<Power> a = MathStuff.factorize(n); //make a list of Power objects from factorize n
    int size = a.size();
    int[] e = new int[size];
    int[] b = new int[size];

    for (int i = 0; i < size; i++) { //collect all the base and exponent of each object in a. This LOOP 1
        if(i >= a.size() || i >= e.length || i > b.length){System.out.print("Out of bounds in LOOP 1");} //test for out of bounds
        e[i] = a.get(i).exponent;
        b[i] = a.get(i).base;
    }

    int g = gcd(e);


    int h = 1; //endproduct base
    for (int i = 1; i < b.length; i++) { //Construct the base by taking the product of each base with its exponent divided by g.
        if(i >= e.length || i >= b.length){System.out.print("Out of bounds in LOOP 3");} //test for out of bounds
        h *= MathStuff.power(b[i], e[i] / g);
    }

    return new Power(h, g); //replace 2

}


/**
 * factorize n
 *
 * @param n the number to 'powerize'
 * @modifies none
 * @pre {@code 2 <= n}
 * @return factorization of n
 */
public static List<Power> factorize(int n) {
    List<Integer> f = new ArrayList<Integer>(); // f are factors
    for (int i = 2; i <= n; i++) {
        while (n % i == 0) {
            f.add(i);
            n /= i;
        }
    }
    //return f; //returns factors

    List<Power> p = new ArrayList<Power>(); // p are the factors with powers
    for (int j = 2; j <= n; j++) { //j will be the base
        int e = 0; //exponent
        for (int k = 0; k <= f.size(); k++) {
            if (f.get(k) == j) {
                e++;
            }
        }
        p.add(new Power(j, e));
    }

    return p; //returns factors in powered form
}

/**
 * gcd returns the greatest common divisor of an integer array
 * @param n
 * @return greatest common divisor
 */
public static int gcd(int... n) {

    //------------------------------------------------THE ERROR OCCURS HERE
    int s = n[0];

    for (int i = 1; i < n.length; i++) {
        if (n[i] < s) {
            s = n[i];
        }
    }

    while (s > 1) {

        int counter = 0;
        int modTot = 0;

        while (counter < n.length) {

            modTot += n[counter] % s;
            counter++;

        }

        if (modTot == 0) {
            return s;
        }

        s--;

    }
    //return 0 if there is no gcd
    return 0;
}   

这里:

public static int gcd(int... n) {
 int s = n[0];

该代码假定 (无需进一步检查gcd()已使用至少一个数组元素调用了gcd() 但是显然那是不正确的。

请记住,您可以像以下那样调用该方法:

gcd(); // NO array at all or
gcd(someArray); // but someArray happens to be null !

因此,对于所有调用gcd()情况,您都必须退后一步并检查源代码。 您必须在其中传递0个参数的地方; 或者您传递实际上为null 的int数组

我假设您的问题是为什么您不能使用size属性设置数组的size

初始化数组(通过new int[x] )时,操作系统将寻找连续的内存块,该内存块应足以容纳x int 找到该内存后,数组的大小将固定为x

想想如果您可以使用size更改数组大小会发生什么。 这意味着,如果要扩展此数组,则必须接管紧随其后的内存。 如果该内存被其他人使用怎么办? 那会带来问题。

但是, java.util.ArrayList在必要时为您解决此问题。 因此,这是您需要的解决方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM