gpt4 book ai didi

java - 如何向 ArrayList 添加元素

转载 作者:行者123 更新时间:2023-12-01 06:25:56 25 4
gpt4 key购买 nike

我怎样才能让用户输入一个数字,然后将数组向右移动1.数组不能超过50。请帮忙,提前致谢:)

  List<Integer> list = new ArrayList<Integer>(1);
public void add(int value) {

list.add(0, value);
for(int i = 0; i < array.length; i++) {
list.add(index, value); // how to make the elements shift to the right?
if(list.size > 50) {
list.remove(50);
}
}
}

最佳答案

 List<Integer> list = new ArrayList<Integer>(50);

public void add(int value) {
if (list.size() == 50)
list.remove(list.size() -1);

list.add(value);
}

关于java - 如何向 ArrayList 添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30231925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com