2017-04-03 73 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