簡體   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