在指定索引处将按钮添加到 ArrayList [英] Add buttons to ArrayList at the specifed index

查看:21
本文介绍了在指定索引处将按钮添加到 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 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>();

我有一个整数变量order",它指示按钮的优先级.当按钮的优先级为 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.

异常出现以下语句:

buttons.add(order,btn);

order 的值大于 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 add 方法的文档..... 说明可能是什么原因...

Check the documentation for ArrayList add method ..... it tells what could be the reason...

http://docs.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆