数组索引出界 - 的Java [英] Array Index Out Of Bounds - Java
问题描述
我已经开始在我的第一个Java程序,这是一个简单的计算器工作,但是我得到一个错误,声称我的数组是出界。我试图调试它,看看哪里,为什么会这样,以及后续在纸上code,两者都显示我希望和祝愿的结果。因此,我看不到的地方实际上,问题在于。在code是不完整的。
根据调试器,在该行发生错误:
答案= outputNum.get(operationIndex)* outputNum.get(operationIndex + 1);
下面是code的主要部分,我目前有:
私有类ButtonHandler实现的ActionListener { / *
*outputNum存储号码顺序输入。
*orderOfOperations存储所有号码顺序乘法和除法之后得到照顾。
*operationList专卖店的数学运算才能输入。
*currentNum存储最近inputed号码。
*currentString存储最近inputed数字串。
*答案存储临时值和最终的答案已输入公式。
*开始商店是否等于按钮已被pssed $ P $和一个新的方程已经开始。
* /
ArrayList的<双> outputNum =新的ArrayList<双> (0);
ArrayList的<双> orderOfOperations =新的ArrayList<双> (0);
ArrayList的<串GT; operationList =新的ArrayList<串GT; (0);
双currentNum = 0.0;
串currentString =0;
双答案= 0.0;
布尔开始= FALSE; 公共无效的actionPerformed(ActionEvent的事件){ 如果(event.getSource()== equalsBtn){ / *获取当前数字串,并将其转换成一个双。
*让操作顺序是已输入订单现在。
*operationIndex来获得不变阵列outputNum和operationList内的值。
*orderIndex来获得改变阵列orderOfOperations内的值。
* /
currentNum = Double.parseDouble(currentString);
outputNum.add(currentNum);
orderOfOperations = outputNum;
INT operationIndex = 0;
INT orderIndex = 0; //通过发生的电流方程中的数学运算周期。
对于(字符串操作:operationList){ 如果(操作==×||操作==÷){ //乘法/除法数与乘法/除法运算在一起。
如果(操作==×){ 回答= outputNum.get(operationIndex)* outputNum.get(operationIndex + 1); }
其他{ 回答= outputNum.get(operationIndex)/ outputNum.get(operationIndex + 1); } //更换数字乘以/在上面的语句划分的新的答案。
orderOfOperations.remove(orderIndex);
orderOfOperations.set(orderIndex,答案);
orderIndex ++; } operationIndex ++; } //重置变量的默认值的新方程。
的System.out.println(orderOfOperations);
outputString = answer.toString();
回答= 0.0;
currentNum = 0.0;
currentString =0;
outputNum.clear();
operationList.clear();
orderOfOperations.clear();
启动= TRUE; }
否则如果(event.getSource()== additionBtn || event.getSource()== subtractionBtn || event.getSource()== multiplicationBtn || event.getSource()== divisionBtn){ //设置文本字段的文本空白,如果这是一个新的方程式的开始。
如果(开始){ outputString =;
开始= FALSE; } / *
*获取当前数字串,并将其转换成一个双。
*添加数学运算并重置当前数目。
* /
currentNum = Double.parseDouble(currentString);
outputNum.add(currentNum);
operationList.add(event.getActionCommand());
currentString =0;
outputString =的String.format(%s%S,outputString,event.getActionCommand()); }
其他{ //设置文本字段的文本空白,如果这是一个新的方程式的开始。
如果(开始){ outputString =;
开始= FALSE; } //添加被inputed按钮值到文本字段的文本以及当前数目。
currentString =的String.format(%s%S,currentString,event.getActionCommand());
outputString =的String.format(%s%S,outputString,event.getActionCommand()); } outputScreen.setText(outputString); } }
下面是我得到的错误信息:
异常螺纹AWT-EventQueue的-0java.lang.IndexOutOfBoundsException:指数:2,大小:2
在java.util.ArrayList.RangeCheck(ArrayList.java:547)
在java.util.ArrayList.get(ArrayList.java:322)
在主窗口$ ButtonHandler.actionPerformed(MainWindow.java:141)
在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
在javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2351)
在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
在javax.swing.DefaultButtonModel.set pressed(DefaultButtonModel.java:242)
在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
在java.awt.Component.processMouseEvent(Component.java:6414)
在javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
在java.awt.Component.processEvent(Component.java:6179)
在java.awt.Container.processEvent(Container.java:2084)
在java.awt.Component.dispatchEventImpl(Component.java:4776)
在java.awt.Container.dispatchEventImpl(Container.java:2142)
在java.awt.Component.dispatchEvent(Component.java:4604)
在java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
在java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
在java.awt.Container.dispatchEventImpl(Container.java:2128)
在java.awt.Window.dispatchEventImpl(Window.java:2492)
在java.awt.Component.dispatchEvent(Component.java:4604)
在java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
在java.awt.EventQueue.access $ 400(EventQueue.java:82)
在java.awt.EventQueue中的$ 2.run(EventQueue.java:676)
在java.awt.EventQueue中的$ 2.run(EventQueue.java:674)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:86)
在java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:97)
在java.awt.EventQueue中的$ 3.run(EventQueue.java:690)
在java.awt.EventQueue中的$ 3.run(EventQueue.java:688)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.AccessControlContext $ 1.doIntersectionPrivilege(AccessControlContext.java:86)
在java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
在 operationIndex
等于在 outputNum $的最后一个元素的索引C $ C>,那么
operationIndex + 1
会比最后一个元素的索引更大:所以你的异常
您会过得更好使用启动索引1 为
循环,这样做的:
//假设operationList和outputNum始终
//有相同数目的元素
的for(int i = 1; I< operationList.size();我++){
...
回答= outputNum.get(I - 1)* outputNum.get(ⅰ);
...
}
,或者,使用您当前的循环,但这样做:
// operationIndex ++; //删除此并使用..
如果(++ operationIndex> = outputNum.size())破;
I have started to work on my first Java program, which is a simple calculator, however I get an error claiming that my array is out of bounds. I have tried to debug it to see where and why that is so, as well as following the code on paper, both of which display the results that I would expect and wish for. Therefore, I can not see where the problem actually lies. The code is not complete.
According to the debugger, the error occurs at this line:
answer = outputNum.get(operationIndex) * outputNum.get(operationIndex+1);
Here is the main part of the code that I currently have:
private class ButtonHandler implements ActionListener {
/*
* "outputNum" stores the numbers entered in order.
* "orderOfOperations" stores all numbers in order after multiplication and division has been taken care of.
* "operationList" stores the mathematical operations entered in order.
* "currentNum" stores the most recently inputed numbers.
* "currentString" stores the most recently inputed string of numbers.
* "answer" stores temporary values and ultimately the answer to the inputed equation.
* "start" stores whether or not the equals button has been pressed and a new equation has started.
*/
ArrayList <Double> outputNum = new ArrayList <Double> (0);
ArrayList <Double> orderOfOperations = new ArrayList <Double> (0);
ArrayList <String> operationList = new ArrayList <String> (0);
Double currentNum = 0.0;
String currentString = "0";
Double answer = 0.0;
boolean start = false;
public void actionPerformed(ActionEvent event) {
if(event.getSource() == equalsBtn) {
/* Takes the current numbers string and convert it into a double.
* Let the order of operations be the inputed order for now.
* "operationIndex" to get values inside of the unchanging arrays "outputNum" and "operationList".
* "orderIndex" to get values inside of the changing array "orderOfOperations".
*/
currentNum = Double.parseDouble(currentString);
outputNum.add(currentNum);
orderOfOperations = outputNum;
int operationIndex = 0;
int orderIndex = 0;
//Cycle through the mathematical operations that occur in the current equation.
for(String operation : operationList) {
if(operation == "x" || operation == "÷") {
// Multiply/divide numbers with the multiply/divide operations together.
if(operation == "x") {
answer = outputNum.get(operationIndex) * outputNum.get(operationIndex+1);
}
else {
answer = outputNum.get(operationIndex) / outputNum.get(operationIndex+1);
}
// Replace numbers multiplied/divided in the above statement with the new answer.
orderOfOperations.remove(orderIndex);
orderOfOperations.set(orderIndex, answer);
orderIndex++;
}
operationIndex++;
}
// Reset variables to the default values for the new equation.
System.out.println(orderOfOperations);
outputString = answer.toString();
answer = 0.0;
currentNum = 0.0;
currentString = "0";
outputNum.clear();
operationList.clear();
orderOfOperations.clear();
start = true;
}
else if(event.getSource() == additionBtn || event.getSource() == subtractionBtn || event.getSource() == multiplicationBtn || event.getSource() == divisionBtn) {
// Sets the text fields text blank if this is the start of a new equation.
if(start) {
outputString = "";
start = false;
}
/*
* Takes the current numbers string and convert it into a double.
* Add the mathematical operation and reset the current number.
*/
currentNum = Double.parseDouble(currentString);
outputNum.add(currentNum);
operationList.add(event.getActionCommand());
currentString = "0";
outputString = String.format("%s%s", outputString, event.getActionCommand());
}
else {
// Sets the text fields text blank if this is the start of a new equation.
if(start) {
outputString = "";
start = false;
}
// Adds the button value to the text field text and the current number being inputed.
currentString = String.format("%s%s", currentString, event.getActionCommand());
outputString = String.format("%s%s", outputString, event.getActionCommand());
}
outputScreen.setText(outputString);
}
}
Here is the error message that I get:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at MainWindow$ButtonHandler.actionPerformed(MainWindow.java:141)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6414)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6179)
at java.awt.Container.processEvent(Container.java:2084)
at java.awt.Component.dispatchEventImpl(Component.java:4776)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
at java.awt.Container.dispatchEventImpl(Container.java:2128)
at java.awt.Window.dispatchEventImpl(Window.java:2492)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:690)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
When operationIndex
is equal to the last element index in outputNum
, then operationIndex + 1
will be greater than the last element index: hence your exception
You would be better off using a for
loop that started at index 1, and doing:
// assuming that operationList and outputNum always
// have the same number of elements
for (int i = 1; i < operationList.size(); i++) {
...
answer = outputNum.get(i - 1) * outputNum.get(i);
...
}
or, alternatively, use your current loop, but do:
// operationIndex++; // remove this and use ..
if (++operationIndex >= outputNum.size()) break;
这篇关于数组索引出界 - 的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!