ArrayLists - PowerPoint PPT Presentation

About This Presentation
Title:

ArrayLists

Description:

The advantage of a ArrayList is that you don't need to know beforehand how big to make it ... an array that you hope is 'big enough' use a ArrayList instead ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 12
Provided by: davidleem
Category:
Tags: arraylists | big

less

Transcript and Presenter's Notes

Title: ArrayLists


1
ArrayLists
2
ArrayLists and arrays
  • A ArrayList is like an array of Objects
  • Differences between arrays and ArrayLists
  • Arrays have special convenience syntax
    ArrayLists dont
  • An array is a fixed size, but a ArrayList expands
    as you add things to it
  • This means you dont need to know the size
    beforehand
  • Arrays can hold primitives or objects, but
    ArrayLists can only hold objects
  • However, autoboxing can make it appear that an
    ArrayList can hold primitives
  • We will discuss autoboxing in some other lecture

3
Creating a ArrayList
  • Specify, in angle brackets after the name, the
    type of object that the class will hold
  • Examples
  • ArrayListltStringgt vec1 new ArrayListltStringgt()
  • ArrayListltStringgt vec2 new ArrayListltStringgt(10)
  • (10 is the initial size you want)
  • This is sort of like an array, where you would
    say String ary new String10instead
    of ArrayListltStringgt ary new
    ArrayListltStringgt(10)

4
Adding elements to a ArrayList
  • boolean add(Type obj)
  • Appends the object obj to the end of this
    ArrayList
  • The obj must be of the correct type, or you get a
    compile-time (syntax) error
  • Always returns true
  • This is for consistency with other, similar
    classes
  • void add(int index, Type element)
  • Inserts the element at position index in this
    ArrayList
  • The index must be greater than or equal to zero
    and less than or equal to the number of elements
    in the ArrayList
  • The obj must be of the correct type, or you get a
    compile-time (syntax) error

5
Removing elements
  • boolean remove(Object obj)
  • Removes the first occurrence of obj from this
    ArrayList
  • Returns true if an element was removed
  • Uses equals to test if it has found the correct
    element
  • void remove(int index)
  • Removes the element at position index from this
    ArrayList
  • void clear()
  • Removes all elements

6
Getting values out
  • Type get(int index)
  • Returns the component at position index
  • Using get
  • ArrayListltStringgt myList new ArrayListltStringgt()
    myList.add("Some string")String s
    myList.get(0)

7
Searching a ArrayList
  • boolean contains(Object element)
  • Tests if element is a component of this ArrayList
  • Uses equals to test if it has found the correct
    element
  • int indexOf(Object element)
  • Returns the index of the first occurrence of
    element in this ArrayList
  • Uses equals to test if it has found the correct
    element
  • Returns -1 if element was not found in this
    ArrayList
  • int lastIndexOf(Object element)
  • Returns the index of the last occurrence of
    element in this ArrayList
  • Uses equals to test if it has found the correct
    element
  • Returns -1 if element was not found in this
    ArrayList

8
Getting information
  • boolean isEmpty()
  • Returns true if this ArrayList has no elements
  • int size()
  • Returns the number of elements currently in this
    ArrayList
  • Type toArray(Type )
  • Returns an array containing all the elements of
    this ArrayList in the correct order
  • The parameter is usually a zero-length array of
    the correct type
  • Example String names nameList.toArray(new
    String0)

9
More about equals
  • There are many different notions of equality
  • Example two sets are equal if they contain the
    same elements order of elements is irrelevant
  • Java defines public boolean equals(Object) in the
    Object class, but
  • equals is defined to be the same as
  • Its often a good idea to override equals for
    your own objects
  • If you do this, note that the argument should be
    a general Object
  • The String class (and some others) override
    equals

10
Conclusion
  • A ArrayList is like an array of Objects
  • The advantage of a ArrayList is that you dont
    need to know beforehand how big to make it
  • The disadvantage of a ArrayList is that you cant
    use the special syntax for arrays
  • You should never use an array that you hope is
    big enoughuse a ArrayList instead

11
The End
Where a calculator on the ENIAC is equipped with
18 000 vacuum tubes and weighs 30 tons, computers
of the future may have only 1 000 vacuum tubes
and perhaps weigh 1½ tons.
--Popular Mechanics, March 1949
Write a Comment
User Comments (0)
About PowerShow.com