130 likes | 328 Views
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:
E N D
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: 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( )
Find Some methods • Go to: • http://java.sun.com/j2se/1.5.0/docs/api/ • Choose ArrayList • Scroll down to methods
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()
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.
Common Methods • Mutator methods • Accessor methods • toString method
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
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)
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
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
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.