按钮添加到ArrayList中为指定的索引 [英] Add buttons to ArrayList at the specifed index
问题描述
我想一个按钮添加到一个ArrayList,但是当我指定索引的错误出现说索引出界。
I am trying to add a button to an ArrayList, but when I specify the index an error comes up saying the index is out of bounds.
我发起数组是这样的:
ArrayList<Button> buttons = new ArrayList<Button>();
我有一个整型变量订单,表示该按钮的优先级。该按钮应该是数组中的第一个值时,它的优先级为0。
I have an Integer variable "order" which indicates the priority of the button. The button should be the first value in the array when it has priority 0.
唯一的例外出现以下语句:
The exception comes up with the following statement:
buttons.add(order,btn);
编辑:
的顺序的值比ArrayList的大小以上。我动态创建的按钮。我怎么能指定数组的大小?
The value of order is more than the size of the ArrayList. I am creating the buttons dynamically. How would I be able to specify the size of the array?
推荐答案
检查ArrayList中的文档添加方法.....它告诉可能是什么原因...
Check the documentation for ArrayList add method ..... it tells what could be the reason...
的http://文档。 oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(int ,E)
订单大概值比ArrayList的规模。顺序打印值将它添加到列表中这将有助于...
Probably value of order is more than the size of the ArrayList. Print value of order before adding it to the list it will help...
这篇关于按钮添加到ArrayList中为指定的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!