AP CSA 4.10 Exercise 1: Shift, Swap, Split

Unit 4: Data Collections · Lesson 4.10 · Exercise 1

Shift, Swap, Split

Algorithms with ArrayLists. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

These three come up on the free response question in some disguise nearly every year. All three are about positions rather than values, which is why an enhanced for loop is no help for any of them.

What to write

  1. Read an integer count, then that many integers into an ArrayList of Integer.
  2. Print the list with the first element moved to the end and everything else shifted left by one.
  3. Print the original list with its first and last elements swapped.
  4. Print a list of only the values at even indexes of the original.
  5. Print a list of only the values at odd indexes of the original.
  6. The original list must be printed unchanged on the last line, so work on copies.

Your program reads

An integer count of at least 2, then that many integers.

Your program prints

The rotated list, the swapped list, the even index list, the odd index list, and the untouched original.

Worked examples

These are the cases you can see. There are more you cannot, and they use different values, so an answer that prints these numbers as constants will fail.

Example 1 input
5
1 2 3 4 5
Example 1 output
[2, 3, 4, 5, 1]
[5, 2, 3, 4, 1]
[1, 3, 5]
[2, 4]
[1, 2, 3, 4, 5]
Example 2 input
2
8 9
Example 2 output
[9, 8]
[9, 8]
[8]
[9]
[8, 9]

Your answer

Main.java

Input for the Run button

Run sends whatever is in the input box below. Submitting runs your program against every test case, including hidden ones with different input.


  

  

Stuck?

Hint 1

remove(0) returns the element it took out, so add(remove(0)) rotates the list in a single line.

Hint 2

A swap needs a third variable to hold the first value, or the assignment overwrites it before you have used it.

Hint 3

new ArrayList(list) copies. Assigning one list variable to another just makes a second name for the same list, and the last line will show it.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]