Presentation is loading. Please wait.

Presentation is loading. Please wait.

The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.

Similar presentations


Presentation on theme: "The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The."— Presentation transcript:

1 The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The ArrayList class is part of the java.util package of the Java standard class library. It works like an array in that it can store a list of values and reference them by an index. But an array remains a fixed size throughout its existence, and an arraylist object grows and shrinks as needed.

2 The ArrayList Class The ArrayList class is part of the java.util package It must be imported Like an array, it can store a list of values and reference them with an index Unlike an array, an ArrayList object grows and shrinks as needed Items can be inserted or removed with a single method invocation It stores references to the Object class, which allows it to store any kind An ArrayList is an object of the ArrayList class. Therefore, to create an ArrayList, you need to use the keyword new along with the constructor from the ArrayList class. You also need to know the data type of the objects that will be stored in the list.

3 DestinysChild.java The DestinysChild program instantiates an ArrayList called band. The method add is used to add several String objects to the ArrayList in a particular order. Then one string is deleted and another is inserted at a particular index. As with any other object, the toString method of the ArrayList class is automatically called when it is sent to the println method. Note that when an element from an ArrayList is deleted, the mist of elements collapses so that there are no holes in the list. Likewise, when an element is inserted the list expands.

4 Declaring an ArrayList Object
Two general forms of declaring anArrayList: ArrayList<ClassName> nameOfArrayList = new ArrayList<ClassName>(); Or List<ClassName> nameofArrayList = ArrayList<ClassName>(); The ArrayList uses a pair of angle brackets to enclose the class name of the objects it will store. An ArrayList always starts out empty. When you create an ArrayList object, it is empty, meaning that there is no item in the list. It’s like when your mom starts to make a “To Do” list and she writes the words “To Do” on the top of a piece of paper. The list is created but there is nothing in the list. An ArrayList is resizable. When your mom writes, “Go grocery shopping” or “Buy awesome video game for favorite child” on her to do list, the size of the list grows. As she completes a task on the list, she crosses it out and the size of the list shrinks. This is exactly how an ArrayList is resized.

5 Specifying an ArrayList Element Type
ArrayList is a generic type, which allows us to specify the type of data each ArrayList should hold For example, ArrayList<Family> holds Family objects

6 Recipe.java The recipe program declares an ArrayList<String>. It stores and prints a list of ingredients for a recipe. The elements are added with the .add method. The list Iterator method on ArrayList returns a ListIterator object that can be used to iterate through the items in the list. Once the iterator is obtained from the ArrayList, the hasNext and next methods are used to loop through each of the elements in the list. We could have also used a foreach loop here.

7 ArrayList Efficiency The ArrayList class is implemented using an array
The code of the ArrayList class automatically expands the array's capacity to accommodate additional elements The array is manipulated so that indexes remain continuous as elements are added or removed If elements are added to and removed from the end of the list, this processing is fairly efficient If elements are inserted and removed from the middle of the list, the elements are constantly being shifted around


Download ppt "The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The."

Similar presentations


Ads by Google