6 Arrays and ArrayLists

 

This lesson covers:

  • Creating arrays to hold a list of data elements
  • Accessing and modifying elements in an array
  • Manipulating arrays using the Arrays class
  • Creating ArrayLists to hold a list of objects
  • Accessing and modifying elements in an ArrayList
  • Understanding the difference between an array and ArrayList

An array is an object that is used to hold a fixed list of data elements (each item in an array is referred to as an element) that are the same data type. They are extremely useful and often used in OO programming. You can think of an array as a list of data elements.  The Java collections API has a list interface that is discussed in a later chapter of this book.

 An array can be a list of numbers, Strings, characters, or even a list of boolean values. In Java, Strings act like primitive data types but they are actually objects.  So, an array can store a list of primitive data or a list of objects. For example, an array can be used to hold the responses of a student who took a true/false quiz, which would be a list of boolean values.  If the quiz had 25 questions, without an array, we would need 25 variables to hold the student responses.  By using an array, we can hold all 25 values with a single variable. 

6.1   Creating arrays

6.2   Access and modify elements in an array

6.3   Manipulating arrays using the Arrays class

6.4   Creating ArrayLists

6.5   Accessing and modifying elements in an ArrayList

6.6   Summary