[PDF] CS 106A Lecture 19 ArrayLists suggested reading: Java Ch. 11.





Previous PDF Next PDF



Lecture 6: ArrayList Implementation

ArrayList<E>. • Standard Java libraries have lots of extra methods not in our implementation. • Many involve working on other collections.



Generic ArrayLists Collections

Oct 3 2016 If the implementation is ... What is the most generic concrete object in Java? ... Implementing ArrayList is almost exactly the same as.



CS 310: ArrayList Implementation

Java has a nice library of containers Collections framework. ? Interfaces that provide get()



Lists -- How to Build Your Own

Your Own. Atul Prakash. Reference: Look at ArrayList.java implementation in Java. Source: http://www.docjar.com/html/api/java/util/ArrayList.java.html 



CS 106A Lecture 19 ArrayLists

suggested reading: Java Ch. 11.8 Know how to store data in and retrieve data from an ArrayList. ... ArrayList<String> myArrayList = new ArrayList<>(); ...



ArrayLists

amongst classes eg the Collection interface is implemented by many classes. (LinkedList



CS 211: Generics Using ArrayList

https://cs.gmu.edu/~kauffman/cs211/generics-arraylist.pdf



Hash Table

an ArrayList based dictionary. One way is to use Java's hashCode() method. ... Implement chaining hash table (open hash table) using ArrayList.



CS 310: ArrayList Implementation

Java has a nice library of containers Collections framework. ? Interfaces that provide get()



Big O & ArrayList

specific implementation of a ADT. Fall 2020 For Java folks an ArrayList is like an array

2ArrayListsHashMapsWearehere

3Learning Goals•KnowhowtostoredatainandretrievedatafromanArrayList.

4Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap

5Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap

7Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap

8Limitations of Arrays•Sizemustbespecifieduponcreation•Can'tadd/remove/insertelementslater•Nobuilt-inmethodsforsearching,etc.•Can'tprintarrayswithoutArrays.toString(orArrays.deepToString)index0123456789value1249-226517-684723

10Our First ArrayListArrayList myArrayList= new ArrayList<>();

11Our First ArrayListArrayList myArrayList= new ArrayList<>();import java.util.*;

12Our First ArrayListArrayList myArrayList= new ArrayList<>();

13Our First ArrayListArrayList myArrayList= new ArrayList<>();

14Our First ArrayListArrayList myArrayList= new ArrayList<>();Type of items your ArrayListwill store.

15Our First ArrayListArrayList myArrayList= new ArrayList<>();

16Our First ArrayListArrayList myArrayList= new ArrayList<>();

17Our First ArrayListArrayList myArrayList= new ArrayList<>();

18Our First ArrayList// Create an (initially empty) listArrayList list = newArrayList<>();

19Our First ArrayList// Create an (initially empty) listArrayList list = newArrayList<>();// Add an element to the backlist.add("Hello");// now size 1"Hello"

20Our First ArrayList// Create an (initially empty) listArrayList list = newArrayList<>();// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2"Hello""Hello""there!"

21Our First ArrayList// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2// Access elements by index (starting at 0!)println(list.get(0));// prints "Hello"println(list.get(1));// prints "there!"println(list);// prints ["Hello", "there!"]"Hello""Hello""there!"

22Our First ArrayList// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2// Access elements by index (starting at 0!)for(inti= 0; i< list.size(); i++) {println(list.get(i));}"Hello""Hello""there!"

23Our First ArrayList// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2// Access elements by index (starting at 0!)for(inti= 0; i< list.size(); i++) {println(list.get(i));}"Hello""Hello""there!"

24Our First ArrayList// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2// Access elements in order (also for arrays!)for(String str: list) {println(str);}"Hello""Hello""there!"

25Iterating Over ArrayLists// Access elements in order (also for arrays!)for(String str: list) {println(str);}// equivalent tofor(inti= 0; i< list.size(); i++) {String str= list.get(i);println(str);}

26Iterating Over ArrayLists// Access elements in order (also for arrays!)for(String str: list) {println(str);}// equivalent tofor(inti= 0; i< list.size(); i++) {String str= list.get(i);println(str);}

27Bad Times with ArrayLists// Create an (initially empty) listArrayList list = newArrayList<>();// Wrong type -bad times! Won't compileGLabellabel = newGLabel("Hello there!");list.add(label);// Invalid index! IndexOutOfBoundsExceptionprintln(list.get(2));

28Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap

29Example: Reversible WritingIamnotapersonwhocontributesAndIrefusetobelievethatIwillbeusefulLet's write a program that reverses a text file.

30Example: Reversible WritingIamnotapersonwhocontributesAndIrefusetobelievethatIwillbeusefulIwillbeusefulAndIrefusetobelievethatIamnotapersonwhocontributesLet's write a program that reverses a text file."I Have a Dream" by AntoniaLee, SaraFung, ChristyFung, RachelLamhttp://poets.spice.org.hk/index.php?option=com_content&view=article&id=45:my-family&catid=6:reverse-poem&Itemid=7

31Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes"

32Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes""AndIrefusetobelievethat"

33Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes""AndIrefusetobelievethat""Iwillbeuseful"Key idea: fill an ArrayListwith each line in the file

34Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes""AndIrefusetobelievethat""Iwillbeuseful"

35Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes""AndIrefusetobelievethat""Iwillbeuseful"Key idea: print the ArrayListitems in reverse order

36Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}

37Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}

38Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}

39Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}

40Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}

41Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}

42Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}

43Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}

44Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap

45ArrayListMethodslist.add(value);appendsvalueatendoflistlist.add(index, value);insertsgivenvaluejustbeforethegivenindex,shiftingsubsequentvaluestotherightlist.clear();removesallelementsofthelistlist.get(index)returnsthevalueatgivenindexlist.indexOf(value)returnsfirstindexwheregivenvalueisfoundinlist(-1ifnotfound)list.isEmpty()returnstrueifthelistcontainsnoelementslist.remove(index);removes/returnsvalueatgivenindex,shiftingsubsequentvaluestotheleftlist.remove(value);removesthefirstoccurrenceofthevalue,ifanylist.set(index, value);replacesvalueatgivenindexwithgivenvaluelist.size()returnsthenumberofelementsinthelistlist.toString()returnsastringrepresentationofthelistsuchas"[3, 42, -7, 15]"

46Insert/remove•Ifyouinsert/removeinthefrontormiddleofalist,elementsshifttofit.list.add(2, 42);•shiftelementsrighttomakeroomforthenewelementlist.remove(1);•shiftelementslefttocoverthespaceleftbytheremovedelementindex01234value38975index012345value3842975index012345value3842975index01234value342975

47Example: Planner

48Example: Planner•Let'swriteaprogramtohelpplanoutourday-Theprogramfirstpromptsforthingsyouwanttodotoday-Then,itaskstheusertore-inputtheminorderofcompletion-Finally,itoutputstheordertheuserhaschosenfortheirtasks

49Planner: Approach"Walk Daisy"Todos:

50Planner: Approach"Walk Daisy""Play Zelda"Todos:

51Planner: Approach"Walk Daisy""Play Zelda""Lunch with Avi"Todos:

52Planner: Approach"Walk Daisy""Play Zelda""Lunch with Avi"Todos:Order:"WalkDaisy"

53Planner: Approach"Play Zelda""Lunch with Avi"Todos:Order:"WalkDaisy"

54Planner: Approach"Play Zelda"Todos:Order:"WalkDaisy""Lunch with Avi"

55Planner: ApproachTodos:Order:"WalkDaisy""Lunch with Avi""Play Zelda"DONE!

56Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap

57ArrayLists+ Primitives = í ½

// Doesn't compile LArrayList list = newArrayList<>();Unlike arrays, ArrayListscan only store objects!

58ArrayLists+ Primitives = í ½

// Use wrapper classes when making an ArrayListArrayList list = newArrayList<>();// Java converts Integer <-> intautomatically!intnum= 123;list.add(num);intfirst = list.get(0);// 123Conversion happens automatically!

60Array vs. ArrayListArrayListArrayList list = new ArrayList<>();list.add(1); // [1]list.add(2); // [1, 2]list.set(0, 3); // [3, 2]intx = list.get(0); // 3list.add(4); // [3, 2, 4]list.contains(2); // trueArrayint[] arr= new int[2]; // [0, 0]arr[0] = 1; // [1, 0]arr[1] = 2; // [1, 2]arr[0] = 3; // [3, 2]intx = arr[0]; // 3[no equivalent]

61Array vs. ArrayListWhydobothoftheseexistinthelanguage?-ArraysareJava'sfundamentaldatastorage-ArrayListisalibrarybuiltontopofanarrayWhenwouldyouchooseanarrayoveranArrayList?-Whenyouneedafixedsizethatyouknowaheadoftime-Simplersyntaxforgetting/setting-Moreefficient-Multi-dimensionalarrays(e.g.images)-Histograms/tallying

62Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap

quotesdbs_dbs20.pdfusesText_26
[PDF] arraylist length java

[PDF] arraylist object java android

[PDF] arraylist object java sort

[PDF] arraylist object javascript

[PDF] arrays in java

[PDF] arrhenius equation activation energy

[PDF] arrhenius equation derivation

[PDF] arrhenius equation example

[PDF] arrhenius equation graph

[PDF] arrhenius equation ln

[PDF] arrhenius equation pdf

[PDF] arrhenius equation r

[PDF] arrhenius equation units

[PDF] arrima

[PDF] arris vip2262 price