/** * Author: Josiah Yoder et al. * Class: SE1011-051 * Lesson: Week 10, Day 2 */ import java.util.ArrayList; import java.util.Scanner; public class Example051_10_2 { /* * This is a scratch-paper method */ public static void main(String[] ignored) { // Design Strategies // pp. 338-355 // Sec. 8.7 - 8.11 // top-down: Write method stubs first // bottom-up: Implement one method at a time // case-based: Modify an old program //========================================== // // See RandomFun051_10_2 for a version of RandomFun // that is re-written to use instance variables instead of class variables // //========================================== //========================================== // // See Complex051_10_2 for a version of Complex // that includes a class variable that increments // every time a method is called. // //========================================== // From last class //(Thank you "Team Awesome") Scanner in = new Scanner(System.in); ArrayList list; list = new ArrayList(); boolean end = false; double real = 0; double imaginary = 0; while(!end){ System.out.print("Input real number: "); real = in.nextDouble(); System.out.print("Input imaginary number: "); imaginary = in.nextDouble(); if(real==0 && imaginary == 0) end = true; else list.add(new Complex051_10_2(real, imaginary)); } ArrayList list2; list2 = list; list2.set(0, new Complex051_10_2(10,10)); // What do the following loops print? // Why? System.out.println("Classic for loop (list):"); for(int i=0; i words = new ArrayList(); words.add("programming"); words.add("fun"); int index = words.indexOf("fun"); System.out.println("The fun is at index "+index); index = words.lastIndexOf("fun"); System.out.println("The fun is still at index "+index); // words.isEmpty() // words.size() == 0 /* When to use ArrayLists? * When taking user input * When you want to keep expanding the array * When you want to delete elements (remove) When to use Arrays? * You know exactly how many objects you will have * You want to store primitive types */ // Can use primitives in ArrayLists, but need to wrap. // See p. 437 (autoboxing) ArrayList numbers = new ArrayList(); numbers.add((double)0); double d = numbers.get(0); } }