第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將元素添加到整數(shù)數(shù)組的開頭

將元素添加到整數(shù)數(shù)組的開頭

GCT1015 2022-05-12 17:24:04
使用 addBefore() 方法,將新元素添加到 int 數(shù)組的開頭,然后使現(xiàn)有元素的索引增加一。這是嘗試運(yùn)行時控制臺中顯示的內(nèi)容——java.lang.RuntimeException: Index 1 應(yīng)該有值 11 但在 IntArrayListTest.main(IntArrayListTest.java:67) 處有 0以下是我到目前為止的代碼。public class IntArrayList {private int[] a; private int length;private int index;private int count;public IntArrayList() {    length = 0;     a = new int[4]; }public int get(int i) {     if (i < 0 || i >= length) {        throw new ArrayIndexOutOfBoundsException(i);    }    return a[i];}public int size() {     return length; }public void set(int i, int x) {    if (i < 0 || i >= a.length) {        throw new ArrayIndexOutOfBoundsException(i);    }    a[i] = x;}public void add(int x) {    if (length >= a.length) {        int[] b = new int[a.length * 2];        for (int i = 0; i < a.length; i++) {            b[i] = a[i];        }        a = b;        //count += 1;    }    a[length] = x;    count++;    length = length + 1;}public void addBefore(int x) {    int[] b = new int[a.length*2];    for (int i = 0; i < a.length; i++) {        b[i+a.length] = a[i];    }    a = b;    a[index] = x;    length ++;    }   }
查看完整描述

3 回答

?
拉丁的傳說

TA貢獻(xiàn)1789條經(jīng)驗 獲得超8個贊

如果答案要求您自己進(jìn)行循環(huán),那么這樣的事情應(yīng)該可以正常工作(執(zhí)行此操作的幾種方法之一,但是是O(n)):


public void addBefore(int x) {

        if(length + 1 >= a.length){

            int[] b = new int[a.length*2];

            b[0] = x;

            for (int i = 0; i < length; i++) {

                b[i + 1] = a[i];

            }

            a = b;

        } else {

            for (int i = length; i >= 0 ; i--) {

                a[i + 1] = a[i];

            }

            a[0] = x;

        }

        length++;

    }

我注意到這開始運(yùn)行“速度測試” - 不確定這樣的測試有多有用,因為它將基于 cpu 性能,而不是測試算法的復(fù)雜性..


查看完整回答
反對 回復(fù) 2022-05-12
?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗 獲得超5個贊

無論您是先添加還是最后添加,只有在數(shù)組已滿時才需要增加數(shù)組大小。


該count字段似乎與 完全相同length,并且index作為一個字段似乎未使用且無意義,因此將它們都刪除。


要重新排列數(shù)組中的值,請使用以下方法:

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)


你的兩個“添加”方法應(yīng)該是:


public class IntArrayList {

    private int[] a; // Underlying array

    private int length; // Number of added elements in a


    // other code


    public void add(int x) {

        if (length == a.length) {

            int[] b = new int[a.length * 2];

            System.arraycopy(a, 0, b, 0, length);

            a = b;

        }

        a[length++] = x;

    }


    public void addBefore(int x) {

        if (length < a.length) {

            System.arraycopy(a, 0, a, 1, length);

        } else {

            int[] b = new int[a.length * 2];

            System.arraycopy(a, 0, b, 1, length);

            a = b;

        }

        a[0] = x;

        length++;

    }

}


查看完整回答
反對 回復(fù) 2022-05-12
?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗 獲得超5個贊

您的解決方案存在三個問題:

  1. 您增加了a每次調(diào)用該方法的長度。這將很快創(chuàng)建一個OutOfMemoryException

  2. 當(dāng)你從ato復(fù)制值時b,你做了b[i+a.length] = a[i];這意味著這些值將被復(fù)制到中間b而不是只移動一個位置

  3. 最后,您將新值放在數(shù)組的末尾而不是開頭。

我之所以能看到這一切,是因為我在您的代碼上使用了調(diào)試器。如果您希望能夠檢測和修復(fù)代碼中的問題,則需要開始使用此工具。

所以固定的解決方案會這樣做:

  1. 檢查是否a已滿(就像使用add()方法完成一樣),如果是,則創(chuàng)建b并將所有內(nèi)容復(fù)制到其中,依此類推)

  2. 將所有值前移一位。最簡單的方法是從長度向后循環(huán)到0

  3. 在數(shù)組的開頭分配新值

這是一個可行的解決方案:

public void addBefore(int x) {


    // increase length if a is full

    if (length >= a.length) {

        int[] b = new int[a.length * 2];

        for (int i = 0; i < a.length; i++) {

            b[i] = a[i];

        }

        a = b;

    }

    // shift all values one cell ahead

    for (int i = length; i > 0; i--) {

        a[i] = a[i-1];

    }


    // add new value as first cell 

    a[0] = x;

    length ++;

    }   

}


查看完整回答
反對 回復(fù) 2022-05-12
  • 3 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號