5

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); 
    }

}
2
  • This may help you.. Commented Sep 3, 2015 at 23:57
  • 1
    So you want to do a circular rotation of the elements? Commented Sep 4, 2015 at 0:29

6 Answers 6

12

Maybe this is an exercise you are working on, but the ArrayList.add(int index,E element) method does almost what you want.

"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.

Sign up to request clarification or add additional context in comments.

Comments

4

A few problems here:

  1. 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 position 0 resulting in out of bounds error.
  2. Setting the first element to be the last should be outside the loop.
  3. listValues.set takes in an index in the list as the first parameter, you are giving it the object in the list

    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);     
    }
    

Comments

2

The easiest and shortest solution : (if you don't have concurrent use of list - Because in concurrent use and iterating on it, the list size should not change, otherwise you get ConcurrentModificationException)

public void shiftOneToRight() {
    
    listValues.add(0, listValues.remove(listValues.size() - 1));
    
}

Comments

2

Here is the simplest solution

Collections.rotate(list, rotationPosition);

Comments

0
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...

Comments

0

input array list: locationMap

shift LHS elements from idxStart in circular way

shifted output list: extendedList

// make extended list to behave like circular
List<String> extendedList = new ArrayList<>();
for (int i = idxStart; i < locationMap.size(); i++) { // current to end
    extendedList.add(locationMap.get(i));
}
for (int i = 0; i < idxStart; i++) { // 0 to current
    extendedList.add(locationMap.get(i));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.