【问题标题】:Reuse StringBuilder and ArrayIndexOutOfBoundsException重用 StringBuilder 和 ArrayIndexOutOfBoundsException
【发布时间】:2017-04-03 16:18:39
【问题描述】:

我最近开始学习 Java,但遇到了 StringBuilder 类的问题。 我有一个StringBuilder 实例,在向它附加许多字符串后,我想清空它以附加一个新序列。

我尝试了许多不同的方法,例如为该变量分配一个新的 StringBuilder,deletesetLength 等等,但我得到了一个异常 ArrayIndexOutOfBoundsException

为什么我会收到此异常?我怎样才能有效地完成这项任务?

编辑:所以详细说一下我尝试编写的计算应用程序,例如 Window caculate app,主要是过程变量将存储数字和操作,因此当它需要结果时它会调用计算方法来做到这一点。它将完全像窗口计算应用程序一样工作。如果我继续重复执行“+”操作,它会正常工作,它会在点击操作按钮后更新结果,但是当点击“=”按钮时,它会在进程变量处获得异常 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                   

}

【问题讨论】:

  • 我认为这是一个稍微不同的问题 b/c 用户得到一个例外而不是想知道执行任务的不同方式对性能的影响。

标签: java indexoutofboundsexception stringbuilder


【解决方案1】:

请尝试创建一个新的空 StringBuilder

sb = new StringBuilder("");

【讨论】:

    【解决方案2】:
        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 2014-11-17
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      • 2016-08-31
      相关资源
      最近更新 更多