1 / 13

ArrayLists

ArrayLists. Section 9.9. ArrayLists. like arrays, hold multiple things not like arrays: size not fixed cannot hold primitives (array can hold both) use methods for EVERYTHING. ArrayList syntax. import java.util.ArrayList To declare:

beyla
Télécharger la présentation

ArrayLists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ArrayLists Section 9.9

  2. ArrayLists • like arrays, hold multiple things • not like arrays: • size not fixed • cannot hold primitives (array can hold both) • use methods for EVERYTHING

  3. ArrayList syntax import java.util.ArrayList • To declare: ArrayList<String> mystrings = new ArrayList<String> ( ); • < >s part of generics, Java 5 • To add: object.add(thingtoadd) mystrings.add("this string"); • Adds at end. you don't specify an index (but you can) • To retrieve: object.get(indexnbr) String astring = mystrings.get(0); // first string • To find the size: object.size( )

  4. Find Some methods • Go to: • http://java.sun.com/j2se/1.5.0/docs/api/ • Choose ArrayList • Scroll down to methods

  5. ArrayList • boolean add(E o) • void add(int index, E element) • void clear() • boolean contains(Object elem) • Eget(int index) • int indexOf(Object elem) • boolean isEmpty() • lastIndexOf(Object elem) • Eremove(int index) • boolean remove(Object o) • E set(int index, E element) • int size()

  6. ArrayList Practice Write a class with a method that calls methods that (each bullet should be a method): • Write a loop to read in 10 strings using JOptionPane.showInputDialog("Enter a string") • Store the Strings in an ArrayList (instance variable) • Print the list • Print all strings beginning with a vowel in the ArrayList. Use charAt in the String class. Remove all strings that begin with “x”. • Print the final list.

  7. Writing Classes

  8. Common Methods • Mutator methods • Accessor methods • toString method

  9. Terms for how classes are used • Server • does something for another class/object • Client • uses a server class to do something • just like in a restaurant, a server often serves many classes (tables in a restaurant) • MemoryGame is a client for Triangle • Triangle serves MemoryGame

  10. Mutator • changes the value of an instance variable (field) public void setColor (String newcolor) { color = newColor; } mutator methods are called setValue mutator methods return void mutator methods have one parameter mutator methods use their parameter value to modify instance variable(s)

  11. Accessor Methods • Accessor methods allow a client to access class information public String getColor( ) { return color; } accessor methods are called getValue accessor methods return a value accessor methods have no parameters accessor methods return the information being requested

  12. toString • The toString method allows an object to be represented as a String (e.g., with the values of the instance variables) public void toString( ) { return ("Triangle is " + color + ", located at (" + xPosition + ", " + yPosition + "with height " + height + ", and width " + width); } must be calledtoSting toString returns void toString does NOT print anything. It creates a String representation

  13. Your turn Write a Grades class that contains an ArrayList of Integers (work like ints, but they are objects), and has the following methods: • a toString method that returns "Grades n1 n2 …" where nk is the value of each grade • two accessor methods: getFirstGrade which returns the first value in the arraylist, and getGrades that returns a String representing the values in the array (call the toString method) • a mutator method setGrade(grade, number) which sets the grade value at the number location. Use set(int index, E element) in java.util.ArrayList • a mutator method addGrade(grade) which adds a grade to the list.

More Related