将新值附加到此列表的末尾。此方法有2种不同的变体。
boolean add(Object value) - 将新值附加到此列表的末尾。
语法
- boolean add(Object value)
参数
- value – Value to be appended to the list.
返回值 - 关于是否添加值的布尔值。
void add(int index,Object value) - 将新值附加到列表中的特定位置。
语法
- void add(int index, Object value)
参数
- value – Value to be appended to the list.
- index- the index where the value needs to be added.
返回值 - 无
例子
下面是一个使用这个方法的例子 -
- class Example {
- static void main(String[] args) {
- def lst = [11, 12, 13, 14];
- println(lst);
- lst.add(15);
- println(lst);
- lst.add(2,20);
- println(lst);
- }
- }
当我们运行上面的程序,我们将得到以下结果 -
- [11, 12, 13, 14]
- [11, 12, 13, 14, 15]
- [11, 12, 20, 13, 14, 15]
转载本站内容时,请务必注明来自W3xue,违者必究。