The default behavior is to add to the end of the list

Info icon This preview shows pages 7–11. Sign up to view the full content.

View Full Document Right Arrow Icon
The default behavior is to add to the end of the list. [hello, ABC, goodbye, okay] The list object keeps track of the element values that have  been added to it, their order, indexes, and its total size. Think of an "array list" as an automatically resizing array object. Internally, the list is implemented using an array and a size field.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 ArrayList  methods (10.1) add( value ) appends value at end of list add( index , value ) inserts given value just before the given index,  shifting subsequent values to the right clear() removes all elements of the list indexOf( value ) returns first index where given value is found  in list (-1 if not found) get( index ) returns the value at given index remove( index ) removes/returns value at given index, shifting  subsequent values to the left set( index , value ) replaces value at given index with given value size() returns the number of elements in list toString() returns a string representation of the list such as  "[3, 42, -7, 15]"
Image of page 8
9 ArrayList  methods 2 addAll( list ) addAll( index , list ) adds all elements from the given list to this list (at the end of the list, or inserts them at the given index) contains( value ) returns true if given value is found somewhere in this list containsAll( list ) returns true if this list contains every element from given list equals( list ) returns true if given other list contains the same elements iterator() listIterator() returns an object used to examine the contents of the list  (seen later) lastIndexOf( value ) returns last index value is found in list (-1 if not found) remove( value ) finds and removes the given value from this list removeAll( list ) removes any elements found in the given list from this list retainAll( list ) removes any elements  not  found in given list from this list subList( from , to ) returns the sub-portion of the list between indexes  from  (inclusive) and  to  (exclusive) toArray() returns the elements in this list as an array
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Type Parameters (Generics) ArrayList< Type > name = new ArrayList< Type >(); When constructing an  ArrayList , you must specify the type of elements it will contain between  <  and  > .
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern