import java.util.ArrayList;
import java.util.*;
ArrayList<Song> playList; // declaration
playList = new ArrayList<Song>(); // creation
Circle
SimpleDate
ArrayList<Song>
void playList.add(Object) // addition at end of list
int playList.size() // returns # of things currently in list
Object playList.get(int index) // selection: returns object at index
Object playList.remove(int index)
ArrayList<Lot> lots; // declaration lots = new ArrayList<Lot>(); // creation Lot newLot = new Lot(nextLotNumber, description); lots.add(newLot); // addition Lot selectedLot = lots.get(lotNumber - 1); // selection
// declaration ArrayList<String> students; // creation: students = new ArrayList<String>(); // addition: students.add("Lee Shepich"); students.add("Jim Hanley"); students.add("Nathan Biller"); System.out.println( students.size() ); // outputs 3 students.add("Chadwick Medendorp"); System.out.println( students.get(2) ); // outputs "Nathan Biller" students.remove(1); // removes "Jim Hanley" System.out.println( students.get(1) ); // outputs "Nathan Biller"
for ( String s : students ) { // whatever you want to do to element s from list e.g. System.out.println( s ); System.out.println("the length of this name is " + s.length() ); } for ( Song s : playList ) { // whatever processing ... } // pages 108-109: for ( Lot lot : lots ) { System.out.println( lot.toString() ); }
int x = 0; while ( x < students.size() ) { String s = students.get(x); System.out.println( s ); System.out.println("the length of this name is " + s.length() ); x++; }
isPrime(int n)
public class util { public boolean isPrime(int n) { int number = 2; while ( number < n ) { if ( n % number == 0 ) { return false; } number++; } // end of while return true; } // end of isPrime() alternatively: public boolean isPrime(int n) { if ( n % 2 == 0 ) { return false; } int number = 3; while ( number < n ) { if ( n % number == 0 ) { return false; } number += 2; } // end of while } // end of isPrime()
ArrayList
while
Iterator<String> sit = students.iterator(); while ( sit.hasNext() ) { String s = sit.next(); System.out.println( s ); System.out.println("the length of this name is " + s.length() ); if ( s.equals("Jacob") ) { sit.remove(); } }
ArrayList array -------- ----- ArrayList<String> notes; String[] notes; notes = new ArrayList<String>(); notes = new String[10]; notes.add(note); notes[3] = note; note = notes.get(3); note = notes[3];
ArrayList.add(...)
int number_of_Jacobs = 0; for ( String s : students ) { // whatever you want to do to element s from list e.g. System.out.println( s ); System.out.println("the length of this name is " + s.length() ); // check whether |s| is equal to "Jacob": // 'equals' ha-ha? if ( s.equals("Jacob") ) { // versus if ( s == "Jacob" ) ... number_of_Jacobs++; } } System.out.println("number of Jacobs: " + number_of_Jacobs);
boolean found_Riddle = false; for ( String s : students ) { // whatever you want to do to element s from list e.g. System.out.println( s ); System.out.println("the length of this name is " + s.length() ); // check whether |s| is equal to "Riddle": // 'equals' ha-ha? if ( s.equals("Riddle") ) { found_Riddle = true; break; } } System.out.println("was there a Riddle? " + found_Riddle);
int i;
i = 35;
int i = 35;
double z = 0.618;
element_type[] array_name = { init_elt0, init_elt1, init_elt2, ... };
int[] data = { 93, 26, 41, 85, 74 };
array_name = new element_type[num._of_elements];
int [] data;
...
data = { 93, 26, 41, 85, 74 };
for
// same functionality as |while| earlier for ( int x = 0; x < students.size(); x++ ) { String s = students.get(x); System.out.println( s ); System.out.println("the length of this name is " + s.length() ); } // textbook page 121: for ( int hour = 0; hour < hourCounts.length; hour++ ) ... for ( int count = 0; count < LIMIT; count++ ) ...