Lists
Lists are ordered collections of data. Unlike sets, lists can have repeated data. Having data contained within lists allows you to perform searches that will give the locations of certain objects within a given list. Given a position, it is possible to directly access an item in a list, add new items, remove items, and even add full lists. Lists are sequential, which makes them easy to navigate using iterators, a feature that will be explored in full in a later section in the chapter. There are also some methods for performing range-based operations on sublists.
There are two different list implementations: ArrayList
and LinkedList
. Each of them is ideal depending on the circumstances. Here, we will work with ArrayList
mainly. Let's start by creating and populating an instance, then search for a certain value, and given its location within the list, we'll print out the value.
import java.util.*; public class Example12 { Â Â Â Â public static void...