亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

調整大小功能時遇到問題

調整大小功能時遇到問題

肥皂起泡泡 2023-07-19 15:47:15
我正在為實驗室編寫一個調整大小的函數,但我不斷收到錯誤線程“main”中的異常 java.lang.OutOfMemoryError:Java 堆空間private E[] a, b;  // holds the itemsprivate int N;       // number of items in stack// create an empty stack with given capacitypublic RArrayStack() {    a = (E[]) new Object[8];    N = 0;}public boolean isEmpty() {    return N == 0;}public boolean isFull() {    return N == a.length;}public void push(E item) {    if (!this.isFull()) {        a[N++] = item;    } else {        this.resize();    }}public E pop() {    return a[--N];}public E peek() {    return a[N - 1];}public E[] resize(){        b = (E[]) new Object[a.length*2];        for (int i = 0; i < a.length ; i++) {            b[i] = a[i];        }        a = b;    return resize();}
查看完整描述

1 回答

?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

在你的resize()函數中:


public E[] resize(){

        b = (E[]) new Object[a.length*2];

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

            b[i] = a[i];

        }

        a = b;


    return resize();

}

您無條件地調用resize()return,這意味著該方法將遞歸,直到您嘗試為 new 分配足夠的內存為止b。而不是return resize(),你想返回a


查看完整回答
反對 回復 2023-07-19
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號