CS Programming I: ArrayList

Similar documents
CS Programming I: Arrays

CS Programming I: Inheritance

CS 302: Introduction to Programming in Java. Lecture 12

CS Programming I: Using Objects

CS Programming I: Exceptions

CS Programming I: Primitives and Expressions

CS Programming I: Using Objects

CS Programming I: Branches

CS Programming I: Branches

CS Programming I: Programming Process

CS Programming I: Programming Process

Binghamton University. CS-140 Fall Chapter 7.7. Lists. Java Library Data Structures

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18

CS Programming I: Exceptions

Array Based Lists. Collections

CS Programming I: Programming Process

Introduction to Programming Using Java (98-388)

CS 211: Using ArrayList, Implementing Arraylist

CS 211: Using ArrayList, Implementing Arraylist

ArrayLists. Chapter 12.1 in Savitch

CS 251 Intermediate Programming Methods and More

CS 200 Objects and ArrayList Jim Williams, PhD

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

CS Programming I: File Input / Output

CS Programming I: File Input / Output

CS 251 Intermediate Programming Methods and Classes

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Java generics. h"p:// h"p://

Inf1-OP. Collections. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 6, School of Informatics

CS159 Midterm #1 Review

AP Computer Science Lists The Array type

PIC 20A Number, Autoboxing, and Unboxing

CS Programming I: Classes

Arrays and ArrayLists. David Greenstein Monta Vista High School

Today: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1

CS 106A, Lecture 19 ArrayLists

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lesson 26: ArrayList (W08D1)

Dynamically sized arrays

CSSE 220 Day 8. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysAndLists from SVN

Using arrays to store data

Programming II (CS300)

Java generics. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Array. Array Declaration:

Notes on access restrictions

Dynamically sized arrays. Overview. The problem with arrays. Java library. The Java Library. Dynamically sized arrays. Normal Java arrays:

Introduction to Computer Science I

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

CSSE 220. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysListPractice from SVN

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Arrays and Array Lists

Adam Blank Lecture 4 Winter 2015 CSE 143. Computer Programming II

Chapter 8. Arrays and Array Lists. Chapter Goals. Chapter Goals. Arrays. Arrays. Arrays

Arrays and ArrayLists. Ananda Gunawardena

CS 101 Spring 2006 Final Exam Name: ID:

Array. Prepared By - Rifat Shahriyar

Lecture 6: ArrayList Implementation

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS 211: Methods, Memory, Equality

ArrayLists. COMP1400 Week 8. Wednesday, 12 September 12

Declaring and ini,alizing 2D arrays

Data Structures Brett Bernstein. Lecture 1: Introduction and a Review/Enrichment of CS 101

CMSC131. A Simple Problem?

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

Computational Expression

APCS Semester #1 Final Exam Practice Problems

Dynamic Arrays. Fundamentals of Computer Science

Examination Questions Midterm 1

1.00/ Lecture 8. Arrays-1

CS 222: Pointers and Manual Memory Management

Intro to Computer Science II

01. Which of the following statement describes dynamic resizing as is applies to the ArrayList class?

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Generics. IRS W-9 Form

generic programming alberto ferrari university of parma

CITS1001 week 4 Grouping objects

Learning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods

Programming Languages and Techniques (CIS120)

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Chapter Seven: Arrays and Array Lists. Chapter Goals

Working with arrays. ArrayLists. Abstraction. Arrays

Java Identifiers, Data Types & Variables

CMSC 341. Nilanjan Banerjee

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Building Java Programs

Computational Expression

Binghamton University. CS-140 Fall Interfaces

Objects and Iterators

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Arrays. CSE 142, Summer 2002 Computer Programming 1.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Transcription:

CS 200 - Programming I: ArrayList Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455

ArrayLists

1/34 Dynamic Arrays arrl: 0 1 2 3 4 5 6 11 34 29 25 42 16 12 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0.

1/34 Dynamic Arrays arrl: 0 1 2 3 4 5 6 7 11 34 29 25 42 16 12 43 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0.

1/34 Dynamic Arrays arrl: 0 1 2 3 4 5 6 7 8 11 34 29 25 42 16 12 43 47 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0.

1/34 Dynamic Arrays arrl: 0 1 2 3 4 5 6 7 8 9 11 34 29 25 42 16 12 43 47 15 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0.

1/34 Dynamic Arrays 0 1 2 3 4 5 6 7 8 9 arrl: 11 34 29 25 42 16 12 43 47 15 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0. Java s java.util.arraylist A dynamic array. Only works with reference types.

2/34 Objects Object Concept Basic Concept: Way to group together related variables and methods. Object-oriented programming: Programming paradigm based on the interaction of objects. E.g. String, Scanner, Integer, etc.

2/34 Objects Object Concept Basic Concept: Way to group together related variables and methods. Object-oriented programming: Programming paradigm based on the interaction of objects. E.g. String, Scanner, Integer, etc. java.lang.object class Every object in Java is derived from the Object class. That is: a String object is an Object. a Scanner object is an Object an Integer object is an Object Primitives are not java.lang.objects

3/34 Creating ArrayList Don t forget: import java.util.arraylist;

3/34 Creating ArrayList Don t forget: import java.util.arraylist; Declaration ArrayList<RefType> arrlist; A reference type. RefType : Notice that contents of the ArrayList is specified between < and >. E.g. ArrayList<Integer> arrlist;

3/34 Creating ArrayList Don t forget: import java.util.arraylist; Declaration ArrayList<RefType> arrlist; A reference type. RefType : Notice that contents of the ArrayList is specified between < and >. E.g. ArrayList<Integer> arrlist; Allocation Constructor with new operator: arrlist = new ArrayList<RefType>();

3/34 Creating ArrayList import java.util.arraylist; Declaration ArrayList<RefType> arrlist; Allocation Constructor with new operator: arrlist = new ArrayList<RefType>(); Some examples: ArrayList < Integer > arrl1 = new ArrayList < Integer >(); ArrayList < String > arrl2 = new ArrayList < String >();

4/34 TopHat Question 1 Will the following compile? import java. util. ArrayList ; public class TopHat1 { } public static void main ( String [] args ) { ArrayList < char > arrl = new ArrayList < char >(); }

5/34 TopHat Question 2 Which statement is true? ArrayList < Double > arrl ; Creates a reference type variable for an ArrayList<Double> called arrl on the stack, and allocates memory for the ArrayList<Double> object on the heap. Creates a reference type variable for an ArrayList<Double> called arrl on the stack.

6/34 Appending an ArrayList Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.

6/34 Appending an ArrayList arrl: ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.

6/34 Appending an ArrayList arrl: ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.

6/34 Appending an ArrayList arrl: 11 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.

6/34 Appending an ArrayList arrl: 11 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.

6/34 Appending an ArrayList arrl: 11 34 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.

6/34 Appending an ArrayList arrl: 11 34 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.

6/34 Appending an ArrayList arrl: 11 34 29 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.

7/34 Automatic Wrapping/Unwrapping int Integer short Short long Long double Double float Float char Character byte Byte boolean Boolean Autoboxing In Java, the automatic conversion between a primitive and its object wrapper class (and vice-versa) is called autoboxing. Boxing Automatically converting a primitive to its object wrapper class. Unboxing Automatically converting an object wrapper class to its primitive value.

8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29);

8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 0 arrl elementdata:

8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 0 arrl elementdata:

8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 1 arrl elementdata: 11

8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 1 arrl elementdata: 11

8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 2 arrl elementdata: 11 34

8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 2 arrl elementdata: 11 34

8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 3 arrl elementdata: 11 34 29

Life of an ArrayList arrl. add (12); arrl. add (42); arrl. add (16); arrl. add (47); arrl. add (43); arrl. add (28); arrl. add (32); arrl. add (20); arrl size: 3 elementdata: 11 34 29 9/34

Life of an ArrayList arrl. add (12); arrl. add (42); arrl. add (16); arrl. add (47); arrl. add (43); arrl. add (28); arrl. add (32); arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32 9/34

Life of an ArrayList arrl. add (12); arrl. add (42); arrl. add (16); arrl. add (47); arrl. add (43); arrl. add (28); arrl. add (32); arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32 9/34

9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32

9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32

9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32

9/34 Life of an ArrayList arrl. add (20); arrl size: 11 elementdata: 11 34 29 12 42 16 47 43 28 32 20

10/34 Capacity Constructor ArrayList<RefType> arrlist = new ArrayList<RefType>(capacity); Allocation with Capacity capacity specifies the initial capacity of the list.

10/34 Capacity Constructor ArrayList<RefType> arrlist = new ArrayList<RefType>(capacity); Allocation with Capacity capacity specifies the initial capacity of the list. TopHat Question 3 With the goal of optimizing the running time of your program, how would you declare an ArrayList, called vals, to store a sequence of measurements (real values) given that there would always be at least 100 measurements?

11/34 Changing an Element in an ArrayList oldobj set(idx, newobj) Params: idx index to replace, and newobj new object. Returns the reference to the replaced object.

11/34 Changing an Element in an ArrayList arrl. set (10, 24); arrl size: 11 elementdata: 11 34 29 12 42 16 47 43 28 32 20

11/34 Changing an Element in an ArrayList arrl. set (10, 24); arrl size: 11 elementdata: 24 11 34 29 12 42 16 47 43 28 32 20

12/34 Removing from an ArrayList remove Methods oldobj remove(idx): Removes and returns the reference at index idx. boolean remove(reftoobj): Removes first occurance of reftoobj if found, returning true on success.

12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 11 elementdata: 11 34 29 12 42 16 47 43 28 32 24

12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32 24

12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 10 elementdata: 11 34 29 12 16 47 43 28 32 24

12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 9 elementdata: 11 34 29 12 16 47 43 28 32 24

13/34 Inserting into an ArrayList Another add Method add(idx, objtoadd) idx insert at index idx. objtoadd object to insert.

13/34 Inserting into an ArrayList arrl. add (5, 15); arrl size: 9 elementdata: 11 34 29 12 16 43 28 32 24

13/34 Inserting into an ArrayList arrl. add (5, 15); arrl size: 10 elementdata: 11 34 29 12 16 15 43 28 32 24

14/34 TopHat Question 4 Which operation is the slowest? Let arrl be an ArrayList with a size of 8 and default capacity. arrl.add(0, 10); arrl.add(10); No difference.

15/34 TopHat Question 5 Which operation is the slowest? Let arrl1 be an ArrayList with a size of 8 and default capacity, and arrl2 be an ArrayList with a size of 10 and default capacity. arrl1.add(10); arrl2.add(10); No difference.

16/34 Other ArrayList Methods refobj get(idx) Returns the reference found at index idx. int size() Returns the number of item in the ArrayList. clear() Removes all the items from the ArrayList. boolean isempty() Returns true if ArrayList is empty.

17/34 Array vs ArrayList Array ArrayList Fixed size Constant access time Works with any type Grows dynamically Constant access time Only works with ref types

17/34 Array vs ArrayList Array ArrayList Fixed size Constant access time Works with any type Grows dynamically Constant access time Only works with ref types int [] arr = {1, 2, 3, 4}; ArrayList < Integer > arrl = arr [0] = 5; new ArrayList < Integer >(); int x = arr [3]; arrl. add (1); arrl. add (2); arrl. add (3); arrl. add (4); arrl. set (0, 5); int x = arrl. get (3);

17/34 Array vs ArrayList Array ArrayList Fixed size Constant access time Works with any type Grows dynamically Constant access time Only works with ref types int [] arr = {1, 2, 3, 4}; ArrayList < Integer > arrl = arr [0] = 5; new ArrayList < Integer >(); int x = arr [3]; arrl. add (1); arrl. add (2); arrl. add (3); arrl. add (4); arrl. set (0, 5); int x = arrl. get (3); arrl. add (5);

18/34 TopHat Question 6 Array or ArrayList? For a classic 3 x 3 tic tac toe game, would you use an array (char[]) or an ArrayList (ArrayList<Character>) to track the moves on the game board?

19/34 TopHat Question 7 Array or ArrayList? Part of a program involves reading in lines from files of any length into an array-type data structure. Would an array (String[]) or an ArrayList (ArrayList<String>) be more suited?

20/34 TopHat Question 8 What is the output? ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); System. out. print ( arrl. get (3). length ());

21/34 Using Objects in ArrayLists ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); Both of the following are equivalent: System. out. print ( arrl. get (3). length ()); String tmpstr = arrl. get (3); System. out. print ( tmpstr. length ());

22/34 TopHat Question 9 What is the output? ArrayList < ArrayList < Integer > > arrl2d = new ArrayList < ArrayList < Integer > >(); for ( int i = 0; i < 3; i ++) { ArrayList < Integer > arrloopy = new ArrayList < Integer >(); for ( int j = i*3 + 1; j <= (i +1)*3; j ++) { arrloopy. add (j); } arrl2d. add ( arrloopy ); } System. out. print ( arrl2d. get (1). get (1));

23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap arrl2d

23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap arrl2d

23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap 4 5 6 1 2 3 7 8 9 arrl2d

23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap 4 5 6 1 2 3 7 8 9 arrl2d 1 2 3 4 5 6 7 8 9

Iterating ArrayLists

24/34 TopHat Question 10 What is the output? a. [1, 2, 3, 4] ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add (1); arrl. add (2); arrl. add (3); arrl. add (4); System. out. print ( arrl ); b. I d like it to print out the array, but I ve see this trick before and it prints out something like java.util.arraylist@e93c3

25/34 TopHat Question 11 Which loop would generally be the most suited for iterating through an ArrayList? a. for b. while c. do while

26/34 Standard Loops import java. util. ArrayList ; public class ArrayListStringSum { public static void main ( String [] args ) { ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); printline ( arrl ); } } public static void printline ( ArrayList < String > line ) { for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } }

27/34 Enhanced for Loop Java s for each Loop for ( type var : collection ) { loopstatement1 ; loopstatement2 ;... loopstatementn ; }

28/34 Enhanced for Loop Example import java. util. ArrayList ; public class ArrayListStringSum { } public static void main ( String [] args ) { ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); printlineredux ( arrl ); } public static void printlineredux ( ArrayList < String > line ) { for ( String word : line ){ System. out. print ( word + " "); } }

29/34 TopHat Question 12 What is the output? import java. util. ArrayList ; public class ArrayListStringSum { } public static void main ( String [] args ) { ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); System. out. print (f( arrl )); } public static int f( ArrayList < String > line ) { int retval = 0; for ( String word : line ) { retval += word. length (); } return retval ; }

30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); }

30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); } Enhanced for loops are more syntactic sugar.

30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); } Enhanced for loops are more syntactic sugar. are quick, clean code to iterate through all the items.

30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); } Enhanced for loops are more syntactic sugar. are quick, clean code to iterate through all the items. prevent out of bounds errors.

30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); } Enhanced for loops are more syntactic sugar. are quick, clean code to iterate through all the items. prevent out of bounds errors. inflexible; lose the position information.

Other Useful Stuff

31/34 Enhanced for and Arrays public static int sumarray ( int [] arr ) { int sum = 0; for ( int val : arr ) { sum += val ; } return sum ; }

32/34 java.util.arrays java.util.arrays Class A collection of static methods for manipulating arrays (copying, sorting, searching, printing,... ).

32/34 java.util.arrays java.util.arrays Class A collection of static methods for manipulating arrays (copying, sorting, searching, printing,... ). ArrayList from an Array ArrayList < Integer > arrl = new ArrayList < Integer >( Arrays. aslist (1,2,3,4)); Integer [] a = {5,6,7,8}; arrl. addall ( Arrays. aslist (a ));

32/34 java.util.arrays java.util.arrays Class A collection of static methods for manipulating arrays (copying, sorting, searching, printing,... ). ArrayList from an Array ArrayList < Integer > arrl = new ArrayList < Integer >( Arrays. aslist (1,2,3,4)); Integer [] a = {5,6,7,8}; arrl. addall ( Arrays. aslist (a )); More ArrayList methods: addall(collection) Appends items addall(idx, collection) Inserts items at idx

33/34 TopHat Question 13 What is the output? ArrayList < ArrayList < Integer > > arrl2d = new ArrayList < ArrayList < Integer > >(); for ( int i = 0; i < 3; i ++) { ArrayList < Integer > arrloopy = new ArrayList < Integer >(); for ( int j = i*3 + 1; j <= (i +1)*3; j ++) { arrloopy. add (j); } arrl2d. add ( arrloopy ); } arrl2d. set (1, arrl2d. get (0)); System. out. print ( arrl2d. get (1). get (1));

34/34 Further Reading COMP SCI 200: Programming I zybooks.com, 2015. zybook code: WISCCOMPSCI200Spring2018 Chapter 9. Objects and ArrayList

Appendix References Appendix

Appendix References References

Appendix References 35/34 Image Sources I https://brand.wisc.edu/web/logos/ http://www.zybooks.com/