Presentation is loading. Please wait.

Presentation is loading. Please wait.

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.

Similar presentations


Presentation on theme: "Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9."— Presentation transcript:

1 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9

2 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 3 ArrayList syntax import java.util.ArrayList To declare: ArrayList mystrings = new ArrayList ( ); – 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 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 4 Find Some methods Go to: –http://java.sun.com/j2se/1.5.0/docs/api/http://java.sun.com/j2se/1.5.0/docs/api/ –Choose ArrayList –Scroll down to methods

5 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 5 ArrayList boolean add(E o)addE void add(int index, E element)addE void clear()clear boolean contains(Object elem)containsObject E get(int index)Eget int indexOf(Object elem)indexOfObject boolean isEmpty()isEmpty lastIndexOf(Object elem)lastIndexOfObject E remove(int index)Eremove boolean remove(Object o)removeObject E set(int index, E element) int size()size

6 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 7 Writing Classes

8 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 8 Common Methods Mutator methods Accessor methods toString method

9 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 10 Mutator changes the value of an instance variable (field) public void setColor (String newcolor) { color = newColor; } mutator methods return void mutator methods have one parameter mutator methods use their parameter value to modify instance variable(s) mutator methods are called setValue

11 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 11 Accessor Methods Accessor methods allow a client to access class information public String getColor( ) { return color; } accessor methods return a value accessor methods have no parameters accessor methods return the information being requested accessor methods are called getValue

12 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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); } toString returns void toString does NOT print anything. It creates a String representation must be called toSting

13 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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.


Download ppt "Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9."

Similar presentations


Ads by Google