所以我有这个代码输入指定数量的项目的订单和价格 公共类SetMenu0 {
private double price;
private int size;
private String output;
private String priceOutput;
ArrayList orderList = new ArrayList<String>();
ArrayList priceList = new ArrayList<Double>();
public SetMenu0()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter New Order?(Y)(N): ");
String init = input.nextLine();
if(init.equalsIgnoreCase("y"))
{
System.out.print("Enter Number of Orders: ");
size = input.nextInt();
for(int i = 0; i<=size; i++)
{
i++;
System.out.print("Enter Order: ");
orderList.add(input.next());
System.out.print("Enter Price: ");
priceList.add(input.nextDouble());
}
}
}
public ArrayList getOrd()
{
return orderList;
}
public ArrayList getPri()
{
return priceList;
}
public int getSize()
{
return size;
}
}
现在当我在TreeMap中使用它时,我在这里得到一个indexOutOfBoundsException public static void main(String [] args) { //空白工作簿 XSSFWorkbook工作簿=新的XSSFWorkbook();
//create blank sheet
XSSFSheet sheet = workbook.createSheet("Food");
SetMenu0 set = new SetMenu0();
ArrayList orderList = set.getOrd();
ArrayList priceList = set.getPri();
int size = orderList.size();
Map<String, Object[]> data = new TreeMap<String, Object[]>();
data.put("1", new Object[]{"Order", "Price"});
try{
for(int i=0; i<size; i++)
{
int in = i+2;
String lineNum = ""+in;
data.put(lineNum, new Object[]{orderList.get(i), priceList.get(i)});
}
}
catch(IndexOutOfBoundsException e)
{
System.out.print("problem");
}
将大小设置为3以上时会发生异常。
答案 0 :(得分:0)
您的问题可能就在这一点:
for(int i = 0; i<=size; i++)
{
i++;
System.out.print("Enter Order: ");
orderList.add(input.next());
System.out.print("Enter Price: ");
priceList.add(input.nextDouble());
}
尝试从循环体中删除i ++。