Java ArrayLists - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Java ArrayLists

Description:

basically you just add, remove, retrieve elements from the list without worrying ... myList.remove(myList.size() - 1); Retrieving Data. simple again. get the ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 11
Provided by: garyg4
Category:

less

Transcript and Presenter's Notes

Title: Java ArrayLists


1
Java ArrayLists
  • Gary Greer

2
What Are ArrayLists?
  • ArrayLists are data storage objects that
  • are unlimited in size
  • perform much of the programming for you
  • also allow you to sort data
  • based on the idea of a list of elements
  • basically you just add, remove, retrieve elements
    from the list without worrying about the size
    (arrays need to be a defined size)

3
Defining and Creating
  • requires import java.util.
  • defining
  • ArrayList myList
  • creating
  • myList new ArrayList
  • simple as that

4
ArrayLists Contain Objects
  • can only use objects, so define one
  • class cat ()
  • String name
  • public cat (String n)
  • name n
  • public String getName ()
  • return name
  • public void setName (String n)
  • name n

5
Adding Data
  • pretty simple
  • create a new object, and then add it to the end
    of the list
  • myCat new Cat ("Stuffy")
  • myList.add(myCat)
  • you can also add to a particular place in the
    list
  • myCat new Cat ("Stuffy")
  • myList.add(3, myCat)

6
Removing Data
  • pretty simple again
  • remove the first element
  • myList.remove(0)
  • remove the last element
  • myList.remove(myList.size() - 1)

7
Retrieving Data
  • simple again
  • get the first element
  • myList.get(0)
  • get the last element
  • myList.get(myList.size() - 1)

8
Working With The Entire List
  • often we need to work with the entire list
  • for example we might want to print it
  • we use for statements to do this
  • for (int index 0 index lt myList.size()
    index)
  • myCat (Cat) myList.get(index)
  • System.out.print(myCat.getName() "
    ")

9
Notes
  • there are much more sophisticated and powerful
    ways to work with ArrayLists
  • iterators are often used to step through the data
  • sorting is possible
  • different types of objects may be stored in the
    same list

10
References
  • Eckel, B. (2003). Thinking in Java. Retrieved
    from www.BruceEckel.com
  • Sun MicroSystems (2005). Java APIs. Retrieved
    from http//java.sun.com/j2se/1.5.0/docs/api/index
    .html
Write a Comment
User Comments (0)
About PowerShow.com