2017-04-03 59 views
0

我最近開始學習Java,並且對StringBuilder類有問題。 我有一個StringBuilder實例,並在我追加了很多字符串後,我想清空它來追加一個新的序列。重用StringBuilder和ArrayIndexOutOfBoundsException

我已經嘗試了許多不同的東西,如爲該變量分配一個新的StringBuilder,delete,setLength和所有這些,但我得到一個異常ArrayIndexOutOfBoundsException

爲什麼我會得到這個異常?我怎樣纔能有效地完成這項任務?

編輯:所以在細節我試圖編程caculate應用程序,比如Window caculate應用,更主要的是過程變量將存儲數量和操作,所以當它需要有一個結果,將CAL caculate方法做那。它將完全像窗口計算應用程序一樣工作。如果我繼續重複執行「+」操作,它會正常工作,它會在點擊操作按鈕後更新結果,但當點擊「=」按鈕時,它將在過程變量中得到異常ArrayIndexOutOfBoundsException。我知道我的代碼是複雜的,希望你們西港島線找到解決方案,我已經嘗試了所有,你們建議的解決方案,所有這一切GES例外

public class Cal extends javax.swing.JFrame { 

/** 
* Creates new form Cal 
*/ 



    StringBuilder process ; 
    StringBuilder textOutcome ; 

    boolean isResult; // it means that you just hit the operation button or "=" button 

    boolean isRestart;// it means after you hit "=" button if after that you continue hit the operation button that will false ,if you hit numbe button it will true 
    public double caculate() 
    { 

     String[] split = process.toString().split(" "); 
     double result = Double.valueOf(split[1]); 
     for(int i = 2 ; i < split.length;i++) 
     { 
      if(split[i].equals("+")) 
      { 
       result += Double.valueOf(split[i + 1]); 
       i += 1; 
      } 


     } 
     return result; 


    } 
public Cal() { 
    initComponents(); 
    process = new StringBuilder(); 
    textOutcome = new StringBuilder(); 

    isResult = true; 
    isRestart = false; 
    GridLayout grid = new GridLayout(4,4,10,10); 


    JButton btnAdd = new JButton("+"); 
     btnAdd.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      //To change body of generated methods, choose Tools | Templates. 
      txtOutcome.setText(String.valueOf(caculate())); 
      process.append(" "+e.getActionCommand()); 
      txtProcess.setText(process.toString()); 
      isResult = true; 
      isRestart = false; 





     } 
    }); 

    pGrid.add(btnAdd); 
    JButton btn4 = new JButton("4"); 
    btn4.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if(isResult) 
       { 
        isResult = false; 
        textOutcome = new StringBuilder(); 
        textOutcome.append(e.getActionCommand()); 
        txtOutcome.setText(textOutcome.toString()); 
        if(isRestart) 
      { 
       process = new StringBuilder(); 

        isRestart = false; 
      } 
        process.append(" "+e.getActionCommand()); 



       } 
      else 
      { 

        textOutcome.append(e.getActionCommand()); 
        txtOutcome.setText(textOutcome.toString()); 

        process.append(e.getActionCommand()); 

      } 

     } 
    }); 
     pGrid.add(btn4); 
     JButton btnResult = new JButton("="); 
     btnResult.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      txtOutcome.setText(String.valueOf(caculate())); 
      process.delete(0, process.length()); 

       process.append(String.valueOf(caculate())); 
       txtProcess.setText(process.toString()); 
       isResult = true; 
       isRestart = true; 

     } 
    }); 

    pGrid.add(btnResult); 
     pGrid.setLayout(grid); 
} 

// Variables declaration - do not modify      
private javax.swing.JPanel jPanel1; 
private javax.swing.JPanel pGrid; 
private javax.swing.JTextField txtOutcome; 
private javax.swing.JTextField txtProcess; 
// End of variables declaration     

}

+2

的可能的複製[StringBuilder的 - 復位或創建一個新的(http://stackoverflow.com/questions/18766780/stringbuilder-reset-or-create-a-new) – JRR

+0

我覺得這是一個slig用戶得到一個異常很不同,而不是想知道執行任務的不同方式對性能的影響。 – WattsInABox

回答

0

請嘗試創建一個新的空的StringBuilder

sb = new StringBuilder(""); 
0
StringBuilder sB = new StringBuilder(); 
    sB.append("Aaa"); 
    sB.append("Bbb"); 
    sB.append("Ccc"); 
    System.out.println(sB); // prints: AaaBbbCcc 

    sB.delete(0, sB.length()); 
    sB.append("Ddd"); 
    System.out.println(sB); //prints: Ddd