DOC PREVIEW
USF CS 112 - ArrayList

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

{small lecturenumber - heblocknumber :} More on I/Oaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} More on I/Oaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} More on I/Oaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} More on I/Oaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} More on I/Oaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} More on I/Oaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} More on Stringsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayListsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Wrapper Classesaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Wrapper Classesaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Integeraddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Autoboxingaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Autoboxingaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Autoboxingaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Autoboxingaddtocounter {blocknumber}{1}Intro to Computer Science IICS112-2012S-05I/O and ArrayListDavid GallesDepartment of Computer ScienceUniversity of San Francisco05-0: More on I/OLots of ways to use Scanner classAlways get more infomation by Googling’Scanner Java’Works for just about any Java library class –first hit will be the Oracle pageRead from a file in addtion to reading from thekeyboardScanner sc = new Scanner(new File("myNumbers"));method nextLine() can get the next line of input inone stringmethod hasNextLine() can determine if there is anext line to read05-1: More on I/OScanner scan = new Scanner(new File("myNumbers"));while (scan.hasNextLine()){String nextLine= scan.nextLine();// Do something with the next line of input}05-2: More on I/OCreating a new File may cause an errorFile might not existConstructor is tagged as a method that may causean errorWe need to either handle the error (we’ll do thislater), or flag our method as one that can cause anerrorThis kind of an error is called an exceptionExample with Eclipse05-3: More on I/Oimport java.io.File;import java.util.Scanner;import java.io.IOException;public static void main(String args[]) throws IOException{Scanner scan = new Scanner(new File("myNumbers"));while (scan.hasNextLine()){String nextLine= scan.nextLine();// Do something with the next line of input}}05-4: More on I/OClass PrintStreamUsed to print to a fileWorks just like System.outConstructor takes a filenmame (technically afilePath)05-5: More on I/Oimport java.io.File;import java.util.Scanner;import java.io.IOException;public static void main(String args[]) throws IOException{Scanner S = new Scanner(new File("test"));PrintStream ps = new PrintStream("testout");while (S.hasNextLine()){String next = S.nextLine();String upper = next.toUpperCase();ps.println(upper);}ps.close();S.close();}05-6: More on StringsLab3Replace strings within a fileCan use replaceAll method of stringCould replace substrings you don’t want toreplace: cat in scatreplaceAll is acceptable, even with this bugFor a slightly more robust version, use splitmethod of string (need to do some work onyour own)05-7: ArrayListsArraylists are similar to python listsClass, with data and methodsMany methods are similar to python listmethodsUnlike python, need to declare type of elementsin the listCan only store class types (no built-in types –no int, bool, etc)05-8: ArrayListsArrayList declaration syntax is a little oddArrayList<TypeName>ArrayList<String> myList = new ArrayList<String>()05-9: ArrayListsArrayList Methods:add(E element): Add element to end of listadd(E element, int index): Add element atlocation ’index’ (shifting elements to the right)get(int index): Get element at location ’index’set(int index, E element): Set element atlocation ’index’ to be ’element’remove(int index): Remove the element atlocation ’index’ (shifting elements to left)size(): Return number of elements in the listFore more, google “ArrayList java” to get oracledocs (example in class)05-10: ArrayListsArrayList<String> myList = new ArrayList<String>();myList.add("Cat");myList.add("Dog");myList.add("Mouse");for (int i = 0; i < myList.size(); i++){System.out.println(myList.get(i));}05-11: ArrayListsWrite a method that takes as an input parameteran unsorted ArrayList of strings and returns thelargest (last in lexigraphic order) stringString findLargest(ArrayList<String> list){// Fill me in!}05-12: ArrayListsWrite a method that takes as an input parameteran unsorted ArrayList of strings and returns thelargest (last in lexigraphic order) stringString findLargest(ArrayList<String> list){if (list.size() == 0){return "";}String largest = list.get(0);for (int i = 1; i < list.size(); i++){if (largest.compareTo(list.get(i)) < 0){largest = list.get(i);}}return largest;}05-13: ArrayListsWrite a method that takes as an input parameter asorted ArrayList of strings and a string to insert,and inserts the new string into the ArrayList inordervoid insertIntoList(ArrayList<String> list, String elem){// Fill me in!}05-14: ArrayListsWrite a method that takes as an input parameter asorted ArrayList of strings and a string to insert,and inserts the new string into the ArrayList inordervoid insertIntoList(ArrayList<String> list, String elem){int index = 0;while(index < list.size() && list.get(index).compareTo(elem) < 0){index++;}list.add(index, elem);}05-15: ArrayListsIn the above method, what happens to duplicates?Next lab, handle duplicates in a different way05-16: Wrapper ClassesThere are a number of “Collection classes, ” Likethe ArrayList class, that can only store otherclasses as data membersWhat if we wanted to store ints, or floats


View Full Document

USF CS 112 - ArrayList

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download ArrayList
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 ArrayList 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 ArrayList 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?