作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我怎样才能让用户输入一个数字,然后将数组向右移动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/
我是一名优秀的程序员,十分优秀!