java 如何在java中将Arraylist中的元素向右移动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32387193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to shift element in an Arraylist to the right in java
提问by Code.girl
So I am trying to create a method that shifts all of the elements in an arraylist to the right and the last element will become the first element. When I run the code, I am told I have an out of bounds error. Here is what I have so far:
所以我试图创建一个方法,将数组列表中的所有元素向右移动,最后一个元素将成为第一个元素。当我运行代码时,我被告知我有一个越界错误。这是我到目前为止所拥有的:
public void shiftRight()
{
//make temp variable to hold last element
int temp = listValues.get(listValues.size()-1);
//make a loop to run through the array list
for(int i = listValues.size()-1; i >= 0; i--)
{
//set the last element to the value of the 2nd to last element
listValues.set(listValues.get(i),listValues.get(i-1));
//set the first element to be the last element
listValues.set(0, temp);
}
}
回答by user949300
Maybe this is an exercise you are working on, but the ArrayList.add(int index,E element)method does almost what you want.
也许这是您正在进行的练习,但ArrayList.add(int index,E element)方法几乎可以满足您的需求。
"Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right(adds one to their indices)." (italics added)
“在此列表中的指定位置插入指定元素。将当前在该位置的元素(如果有)和任何后续元素向右移动(向它们的索引添加一个)。” (加斜体)
So just add the last element in the list at position 0. And delete it from the end.
因此,只需在位置 0 处添加列表中的最后一个元素。并从末尾删除它。
回答by user2718281
A few problems here:
这里有几个问题:
- Your for loop condition needs to exclude the zeroth element so it should be
i > 0
otherwise you'll get to the point where you want to put element at position-1
to position0
resulting in out of bounds error. - Setting the first element to be the last should be outside the loop.
listValues.set
takes in an index in the list as the first parameter, you are giving it the object in the listpublic void shiftRight() { //make temp variable to hold last element int temp = listValues.get(listValues.size()-1); //make a loop to run through the array list for(int i = listValues.size()-1; i > 0; i--) { //set the last element to the value of the 2nd to last element listValues.set(i,listValues.get(i-1)); } //set the first element to be the last element listValues.set(0, temp); }
- 您的 for 循环条件需要排除第零个元素,因此应该是这样,
i > 0
否则您将到达要将元素放在一个位置-1
到另一个位置的位置,0
从而导致越界错误。 - 将第一个元素设置为最后一个应该在循环之外。
listValues.set
将列表中的一个索引作为第一个参数,你给它列表中的对象public void shiftRight() { //make temp variable to hold last element int temp = listValues.get(listValues.size()-1); //make a loop to run through the array list for(int i = listValues.size()-1; i > 0; i--) { //set the last element to the value of the 2nd to last element listValues.set(i,listValues.get(i-1)); } //set the first element to be the last element listValues.set(0, temp); }
回答by Dan Alboteanu
my code to put a number in the right place in a List
int nr = 5; // just a test number
boolean foundPlace = false;
for(int i = 0; i < integerList.size(); i++){
if(nr <= integerList.get(i)){
integerList.add(i,nr);
foundPlace = true;
break;
}
}
if (!foundPlace)
integerList.add(integerList.size(), nr);
as the guy above said, "integerList.add(element)" inserts the specified element at the specified position in this list. Shifts the element currently...
正如上面那个人所说,“integerList.add(element)”在此列表中的指定位置插入指定元素。移动当前元素...