Lesson on 2015 MCQ Q39, List with Alex, Bob, and Carl (List Element Manipulation) STUDENT COPY
A brief lesson on list manipulation that ties into the commonly missed Question 39 on the 2015 MCQ
What are Lists in Java?
I won’t be going super in depth here for the sake of time, but “Lists” in Java refers to the List interface, which many different strucutres in Java employ.
Known examples are:
- Linked Lists
- Vectors
- ArrayLists (What we will be using in this lesson)
I won’t be going over linked lists and vectors in this lesson, but you can find very good and comprehensive explanations for each of them here:
There are many more examples of things that use the List interface in Java…
How can one manipulate lists/arrayLists?
To start, it’s important to define what we can manipulate in these arraylists/lists. I’ll be skipping over creating lists, as that should be pretty basic knowledge by now and if you maybe forgot how to create a list, you can check out this article for more info.
Using java, we can easily manipulate lists by:
- Adding elements
- Updating elements
- Removing elements
- Sorting elements
- Reversing elements
- Checking the size of all elements
- Clearing elements
- Converting the list into an array
There are many more ways to manipulate lists as well, but for the sake of brevity/time, we will only be going over adding, updating, removing, sorting, and finding the size of lists.
Adding elements to a list
In order to add an element to a list in Java, you need to do:
- list_name.add(whatever you wanted to add)
To add an element to a specific index, you need to do:
- list_name.add(index, whatever you wanted to add)
Remember, if you want to add a string, denote so with quotation marks. If you want to add an integer or double, ditch the quotation marks.
You try! In the below code cell, add something to this array list!
List<Integer> myList=new ArrayList<Integer>();
// Add your code!
Removing elements in a list
In order to remove an element to a list in Java, you need to do:
- list_name.remove(element you wish to remove)
In order to remomve an element at a specific index, you need to do:
- list_name.remove(index)
Updating elements in a list
Now say you want to update a value in a list, for that, you would need to do:
- list_name.set(index, what you want to change the element to)
You try! In the below code cell, update the value at index 4 to return 5.
List<Integer> list = Arrays.asList(1,2,3,4,11,6,7); // Here, I'm creating an ArrayList by defining an immutable list using Arrays.asList()
List<Integer> myList = new ArrayList<>(list); // And I'm making it a mutable ArrayList by creating an ArrayList based on the values from the immutable list.
//This method is used later in the hacks section too.
Sorting elements in a list
Now say you have a list of values that are a jumbled up mess, and you would like to view the data ordered. Good news, it’s super easy to do that when working with the Java list interface, due to the Java Collections framework. Just do:
- Collections.sort(list_name)
Using Collections.sort, you can also sort in descending order like so:
- Collections.sort(list_name, Collections.reverseOrder())
Alternatively, you can also use list_name.sort() to achieve the same thing past Java 8, as that’s when .sort() became a default method in the Java List interface.
Finding the size of a list
If you would want to find the size of a list, you can use:
- list_name.size()
And to display the size, just set this equal to an integer variable, as so:
- int size = list_name.size()
MCQ Problem Breakdown:
Now that you’ve learned the basics of list manipulation, it’s time to tie this into the 2015 CollegeBoard MCQ we took. Today, we will be going over question 39. which is the following:
Lets start by trying to solve this individually first without the Multiple Choice. It’s ok if you get this wrong, I won’t be grading this part on whether it’s correct or not, just give it an honest effort. Write your answer in the markdown cell below and write your inital thought process. Think of this as a Popcorn Hack. You have 2 minutes.
Ok, now that you’ve all done that, let’s break down the problem. So first an ___ is being created, with the strings Alex, Bob, and Carl being added.
Next, there is a for loop, which iterates by an interval of 1 until it goes through the entire list, the size being determined by _____.
It changes the value of all the elements to “Alex” using __, while printing what was originally there. This returns ___.
The second for loop simply iterates through all of the “students” ArrayList via an enhanced for loop, and prints out the now updated values in the for loop. This returns ___.
This problem may seem a bit tricky at first, but when you slow down and break the code down like how I’ve shown, it’s pretty simple.