我正在尝试创建一个方法,将ArrayList中的所有元素向右移,并将最后一个元素变为第一个元素。当我运行代码时,会提示我出现了越界错误。以下是我的代码:
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);
}
}