Unformatted text preview:

//Lecture 1 //March 31st, 2014 • 1. Array limitations: fixed size, adding or removing from middle is hard,not much built-in functionality (needs Arrays class), • 2. Collections and lists -collection: an object that stores data("elements") -list: collection of elements with 0-based indexes -elements can be added to the front, back, or elsewhere -a list has a size(number of elements that have been added) -in Java, list can be represented as an ArrayList Object • 3. Type parameters (generics) ArrayList<Type> name = new ArrayList<Type>(); • 4. ArrayList methods -add(value): appends value at end of list -add(index, value): inserts given value just before the given index, shifting subsequent values to the right -clear(): removes all elements of the list -indexOf(value): returns first index where given value is found in list (-1 if not found) -get(index): returns the value at given index -remove(index):removes/returns value at given idex, shifting subsequence values to the left -set(index, value): replaces value at given index with given value -size(): returns number of elements in list -toString(): returns string as "[num, num]" import java.util.*; public class Lecture1 { public static void main(String[] args) { //String[] people = new String[6]; //Using an Array is bad because it constrains the size before number of elements is even known. ArrayList<String> people = new ArrayList<String>(); people.add("Robert"); System.out.println(people); System.out.println(people.size()); } }//ArrayList basically same as Array, just that size is not constrained prior. public class ReverseFile{ public static void main(String[] args) throws FileNotFoundException{ //Capitalize all plural words for (int i = 0; i < words.size(); i++) { if (words.get(i).endsWith("s")) { words.set(i, words.get(i).toUpperCase()); } } //Remove all plural words. THIS DOES NOT WORK BECAUSE everything slides over to the left //when you remove an element. for (int i = 0; i < words.size(); i++) { if (words.get(i).endsWith("s")) { words.remove(i); i--; //HAVE TO ADD THIS } } System.out.println(words); for (int i = words.size() - 1; i >= 0; i--) { System.out.println(words.get(i)); } } //Must include the throws in whatever scans a file! public static ArrayList<String> fileToArrayList(String fileName) throws FileNotFoundException { Scanner input = new Scanner(new File(fileName)); ArrayList<String> words = new ArrayList<String>(); while (input.hasNext()) { words.add(input.next()); } }


View Full Document

UW CSE 143 - Notes

Documents in this Course
Load more
Download Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?