CS Programming I: ArrayList
|
|
- Ralph Gardner
- 6 years ago
- Views:
Transcription
1 CS Programming I: ArrayList Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: TopHat Sec 4 (PM) Join Code:
2 ArrayLists
3 1/34 Dynamic Arrays arrl: 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.
4 1/34 Dynamic Arrays arrl: 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.
5 1/34 Dynamic Arrays arrl: 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.
6 1/34 Dynamic Arrays arrl: 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.
7 1/34 Dynamic Arrays arrl: 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.
8 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.
9 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
10 3/34 Creating ArrayList Don t forget: import java.util.arraylist;
11 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;
12 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>();
13 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 >();
14 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 >(); }
15 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.
16 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.
17 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.
18 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.
19 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.
20 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.
21 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.
22 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.
23 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.
24 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.
25 8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29);
26 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:
27 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:
28 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
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: 1 arrl elementdata: 11
30 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
31 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
32 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:
33 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: /34
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: /34
35 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: /34
36 9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata:
37 9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata:
38 9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata:
39 9/34 Life of an ArrayList arrl. add (20); arrl size: 11 elementdata:
40 10/34 Capacity Constructor ArrayList<RefType> arrlist = new ArrayList<RefType>(capacity); Allocation with Capacity capacity specifies the initial capacity of the list.
41 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?
42 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.
43 11/34 Changing an Element in an ArrayList arrl. set (10, 24); arrl size: 11 elementdata:
44 11/34 Changing an Element in an ArrayList arrl. set (10, 24); arrl size: 11 elementdata:
45 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.
46 12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 11 elementdata:
47 12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 10 elementdata:
48 12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 10 elementdata:
49 12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 9 elementdata:
50 13/34 Inserting into an ArrayList Another add Method add(idx, objtoadd) idx insert at index idx. objtoadd object to insert.
51 13/34 Inserting into an ArrayList arrl. add (5, 15); arrl size: 9 elementdata:
52 13/34 Inserting into an ArrayList arrl. add (5, 15); arrl size: 10 elementdata:
53 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.
54 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.
55 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.
56 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
57 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);
58 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);
59 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?
60 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?
61 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 ());
62 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 ());
63 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));
64 23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap arrl2d
65 23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap arrl2d
66 23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap arrl2d
67 23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap arrl2d
68 Iterating ArrayLists
69 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
70 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
71 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 (" "); } }
72 27/34 Enhanced for Loop Java s for each Loop for ( type var : collection ) { loopstatement1 ; loopstatement2 ;... loopstatementn ; }
73 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 + " "); } }
74 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 ; }
75 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 + " "); }
76 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.
77 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.
78 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.
79 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.
80 Other Useful Stuff
81 31/34 Enhanced for and Arrays public static int sumarray ( int [] arr ) { int sum = 0; for ( int val : arr ) { sum += val ; } return sum ; }
82 32/34 java.util.arrays java.util.arrays Class A collection of static methods for manipulating arrays (copying, sorting, searching, printing,... ).
83 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 ));
84 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
85 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));
86 34/34 Further Reading COMP SCI 200: Programming I zybooks.com, zybook code: WISCCOMPSCI200Spring2018 Chapter 9. Objects and ArrayList
87 Appendix References Appendix
88 Appendix References References
89 Appendix References 35/34 Image Sources I
CS Programming I: Arrays
CS 200 - Programming I: Arrays Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Array Basics
More informationCS Programming I: Inheritance
CS 200 - Programming I: Inheritance Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Inheritance
More informationCS 302: Introduction to Programming in Java. Lecture 12
CS 302: Introduction to Programming in Java Lecture 12 1 Review What is the 3-step processing for using Objects (think Scanner and Random)? Do objects use static methods or non-static (how do you know)?
More informationCS Programming I: Using Objects
CS 200 - Programming I: Using Objects 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 Binary
More informationCS Programming I: Exceptions
CS 200 - Programming I: 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 Command-Line Arguments
More informationCS Programming I: Primitives and Expressions
CS 200 - Programming I: Primitives and Expressions 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:
More informationCS Programming I: Using Objects
CS 200 - Programming I: Using Objects Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Binary
More informationCS Programming I: Branches
CS 200 - Programming I: Branches Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2018 TopHat Sec 3 (AM) Join Code: 925964 TopHat Sec 4 (PM) Join Code: 259495 Boolean Statements
More informationCS Programming I: Branches
CS 200 - Programming I: Branches Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Boolean Statements
More informationCS Programming I: Programming Process
CS 200 - Programming I: Programming Process 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
More informationCS Programming I: Programming Process
CS 200 - Programming I: Programming Process Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2019 TopHat Sec 3 (AM) Join Code: 560900 TopHat Sec 4 (PM) Join Code: 751425
More informationBinghamton University. CS-140 Fall Chapter 7.7. Lists. Java Library Data Structures
Chapter 7.7 Lists Java Library Data Structures 1 Java Library Data Structures A data structure is a way of organizing data The Java Library supports many different data structures Let s start with one
More informationArray Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18
Array Lists CSE 1310 Introduction to Computers and Programming University of Texas at Arlington Last modified: 4/17/18 1 DEPARTAMENTAL FINAL EXAM Monday, DEC 10, 5:30pm-8pm rooms will be determined 2 Fixed
More informationCS Programming I: Exceptions
CS 200 - Programming I: Exceptions Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Command-Line
More informationArray Based Lists. Collections
Array Based Lists Reading: RS Chapter 15 1 Collections Data structures stores elements in a manner that makes it easy for a client to work with the elements Specific collections are specialized for particular
More informationCS Programming I: Programming Process
CS 200 - Programming I: Programming Process Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationCS 211: Using ArrayList, Implementing Arraylist
CS 211: Using ArrayList, Implementing Arraylist Chris Kauffman Week 12-1 Collections Java has a nice library of containers, Collections framework Interfaces that provide get, set, add methds, conversion
More informationCS 211: Using ArrayList, Implementing Arraylist
CS 211: Using ArrayList, Implementing Arraylist Chris Kauffman Week 12-1 Front Matter Goals Today ArrayList Use ArrayList Implementation Reading Lab Manual Ch 17: Generics BJP Ch 10.1: Using ArrayList
More informationArrayLists. Chapter 12.1 in Savitch
ArrayLists Chapter 12.1 in Savitch Using arrays to store data q Arrays: store multiple values of the same type. q Conveniently refer to items by their index q Need to know the size before declaring them:
More informationCS 251 Intermediate Programming Methods and More
CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationCS 200 Objects and ArrayList Jim Williams, PhD
CS 200 Objects and ArrayList Jim Williams, PhD This Week 1. Academic Integrity 2. BP1: Milestone 2 due this week 3. Team Lab: Multi-Dimensional Arrays a. Bring paper and pencil to draw diagrams. b. Code
More informationAdam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods
Adam Blank Lecture 2 Winter 2019 CS 2 Introduction to Programming Methods CS 2: Introduction to Programming Methods File I/O, Object Oriented Programming, and Lists Questions From Last Time 1 What are
More informationCSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists
CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists 1 A programming problem Consider the following task: Double values representing grades are read until the user enters a negative
More informationCOMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code
COMPUTER SCIENCE DEPARTMENT PICNIC Welcome to the 2016-2017 Academic year! Meet your faculty, department staff, and fellow students in a social setting. Food and drink will be provided. When: Saturday,
More informationCS Programming I: File Input / Output
CS 200 - Programming I: File Input / Output Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624
More informationCS Programming I: File Input / Output
CS 200 - Programming I: File Input / Output 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
More informationCS 251 Intermediate Programming Methods and Classes
CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationContents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance
Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass
More informationObject Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.
CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java
More informationJava generics. h"p:// h"p://
Java generics h"p://www.flickr.com/photos/pdxdiver/4917853457/ h"p://www.flickr.com/photos/paj/4002324674/ CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014 Overview Abstract
More informationInf1-OP. Collections. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 6, School of Informatics
Inf1-OP Collections Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics March 6, 2017 Rigidity of arrays Length of array is fixed at creation time. Can
More informationCS159 Midterm #1 Review
Name: CS159 Midterm #1 Review 1. Choose the best answer for each of the following multiple choice questions. (a) What is the effect of declaring a class member to be static? It means that the member cannot
More informationAP Computer Science Lists The Array type
AP Computer Science Lists There are two types of Lists in Java that are commonly used: Arrays and ArrayLists. Both types of list structures allow a user to store ordered collections of data, but there
More informationPIC 20A Number, Autoboxing, and Unboxing
PIC 20A Number, Autoboxing, and Unboxing Ernest Ryu UCLA Mathematics Last edited: October 27, 2017 Illustrative example Consider the function that can take in any object. public static void printclassandobj
More informationCS Programming I: Classes
CS 200 - Programming I: Classes Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Classes 1/23
More informationArrays and ArrayLists. David Greenstein Monta Vista High School
Arrays and ArrayLists David Greenstein Monta Vista High School Array An array is a block of consecutive memory locations that hold values of the same data type. Individual locations are called array s
More informationToday: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1
Today: Java Library Classes for lists. Iterators, ListIterators CS61B Lecture #7 Last modified: Fri Sep 12 14:41:31 2008 CS61B: Lecture #7 1 Abstracting Listness So far, we ve seen fairly primitive types
More informationCS 106A, Lecture 19 ArrayLists
CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights
More informationAssoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.
Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Up to here Not included in program Java collections framework prebuilt data structures interfaces and methods for manipulating
More informationLesson 26: ArrayList (W08D1)
Lesson 26: ArrayList (W08D1) Balboa High School Michael Ferraro October 5, 2015 1 / 25 Do Now Prepare PS #4a (paper form) for pick-up! Consider the code below for powiter(), an iterative algorithm that
More informationDynamically sized arrays
Dynamically sized arrays CSCI 135: Fundamentals of Computer Science Keith Vertanen Copyright 2013 The Java Library Overview Many handy classes and methods Importing a package Dynamically sized arrays Java
More informationCSSE 220 Day 8. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysAndLists from SVN
CSSE 220 Day 8 Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop Check out ArraysAndLists from SVN Questions? Exam 1 is October 1, 7-9 PM! Over chapters 1-7 You'll have a chance to ask
More informationUsing arrays to store data
ArrayLists Using arrays to store data Arrays: store multiple values of the same type. Conveniently refer to items by their index Need to know the size before declaring them: int[] numbers = new int[100];
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management
More informationJava generics. CSCI 136: Fundamentals of Computer Science II Keith Vertanen
Java generics h"p://www.flickr.com/photos/pdxdiver/4917853457/ h"p://www.flickr.com/photos/paj/4002324674/ CSCI 136: Fundamentals of Computer Science II Keith Vertanen Overview Abstract Data Types (ADTs)
More informationJava classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.
CSM 61B Abstract Classes & Interfaces Spring 2017 Week 5: February 13, 2017 1 An Appealing Appetizer 1.1 public interface Consumable { public void consume (); public abstract class Food implements Consumable
More informationArray. Array Declaration:
Array Arrays are continuous memory locations having fixed size. Where we require storing multiple data elements under single name, there we can use arrays. Arrays are homogenous in nature. It means and
More informationNotes on access restrictions
Notes on access restrictions A source code file MyClass.java is a compilation unit and can contain at most one public class. Furthermore, if there is a public class in that file, it must be called MyClass.
More informationDynamically sized arrays. Overview. The problem with arrays. Java library. The Java Library. Dynamically sized arrays. Normal Java arrays:
Dynamically sized arrays The Java Library Overview Many handy classes and methods Importing a package Dynamically sized arrays Java ArrayList Wrapper classes for primitive types CSCI 135: Fundamentals
More informationIntroduction to Computer Science I
Introduction to Computer Science I Iterators ArrayList Janyl Jumadinova October 31, 2016 Iterators One of the most useful operations for any collection is the ability to run through each of the elements
More informationAdam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II
Adam Blank Lecture 5 Winter 2015 CSE 143 Computer Programming II CSE 143: Computer Programming II Stacks & Queues Questions From Last Time 1 Can we include implementation details in the inside comments
More informationCSSE 220. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysListPractice from SVN
CSSE 220 Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop Check out ArraysListPractice from SVN Help with Peers Having a peer help you with some strange bug or specific problem Great
More informationObject Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang
Object Oriented Programming and Design in Java Session 2 Instructor: Bert Huang Announcements TA: Yipeng Huang, yh2315, Mon 4-6 OH on MICE clarification Next Monday's class canceled for Distinguished Lecture:
More informationArrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington
Arrays and Array Lists CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington 1 Motivation Current limitation: We cannot record multiple
More informationArrays and Array Lists
Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter 8 Fall 2006 Slides adapted from Java Concepts companion slides 1 Lecture Goals To become familiar with using
More informationAdam Blank Lecture 4 Winter 2015 CSE 143. Computer Programming II
Adam Blank Lecture 4 Winter 2015 CSE 143 Computer Programming II CSE 143: Computer Programming II Efficiency; Interfaces Questions From Last Time 1 Does a constructor have to use all the fields specified
More informationChapter 8. Arrays and Array Lists. Chapter Goals. Chapter Goals. Arrays. Arrays. Arrays
Chapter 8 Arrays and Array Lists Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the generalized for loop To study common array algorithms
More informationArrays and ArrayLists. Ananda Gunawardena
Arrays and ArrayLists Ananda Gunawardena Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages Arrays allow us to store arbitrary sized sequences
More informationCS 101 Spring 2006 Final Exam Name: ID:
This exam is open text book but closed-notes, closed-calculator, closed-neighbor, etc. Unlike the midterm exams, you have a full 3 hours to work on this exam. Please sign the honor pledge here: Page 1
More informationArray. Prepared By - Rifat Shahriyar
Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types
More informationLecture 6: ArrayList Implementation
Lecture 6: ArrayList Implementation CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Programming Assignment Weak AI/Natural Language Processing: Generate text by building frequency lists based
More informationThe ArrayList class CSC 123 Fall 2018 Howard Rosenthal
The ArrayList class CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Describe the ArrayList class Discuss important methods of this class Describe how it can be used in modeling Much of the information
More informationAgenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda
More informationCS 211: Methods, Memory, Equality
CS 211: Methods, Memory, Equality Chris Kauffman Week 2-1 So far... Comments Statements/Expressions Variable Types little types, what about Big types? Assignment Basic Output (Input?) Conditionals (if-else)
More informationArrayLists. COMP1400 Week 8. Wednesday, 12 September 12
ArrayLists COMP1400 Week 8 Working with arrays There are a number of common actions we want to do with arrays: adding and deleting copying looking for a particular element counting the elements Arrays
More informationDeclaring and ini,alizing 2D arrays
Declaring and ini,alizing 2D arrays 4 2D Arrays (Savitch, Chapter 7.5) TOPICS Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe Game // se2ng up a 2D array final int M=3, N=4;
More informationData Structures Brett Bernstein. Lecture 1: Introduction and a Review/Enrichment of CS 101
Data Structures Brett Bernstein Lecture 1: Introduction and a Review/Enrichment of CS 101 Introduction Class Information The title of this course is Data Structures, but in reality the name only tells
More informationCMSC131. A Simple Problem?
CMSC131 Intro to Generic Data Structures: ArrayList A Simple Problem? What if I asked you to write a program that would allow the user to specify how many numbers they wanted to enter, then read them in,
More informationCS121/IS223. Object Reference Variables. Dr Olly Gotel
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223
More informationInstalling Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites
DP Computer Science Unit 4.3: Intro to programming Installing Java Software & websites JDK (Java development kit) download: http://www.oracle.com/technetwork/java/javase/ Tradition For a full IDE (integrated
More informationComputational Expression
Computational Expression ArrayList Iterators Janyl Jumadinova 7-14 November, 2018 Janyl Jumadinova Computational Expression 7-14 November, 2018 1 / 11 Collections Collection: an object that stores data;
More informationAPCS Semester #1 Final Exam Practice Problems
Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester
More informationDynamic Arrays. Fundamentals of Computer Science
Dynamic Arrays Fundamentals of Computer Science Outline Dynamically sized arrays Java ArrayList Like an array but extra-powerful Has no fixed sized Add/remove elements dynamically as needed Contains objects
More informationExamination Questions Midterm 1
CS1102s Data Structures and Algorithms 10/2/2010 Examination Questions Midterm 1 This examination question booklet has 9 pages, including this cover page, and contains 15 questions. You have 40 minutes
More information1.00/ Lecture 8. Arrays-1
1.00/1.001 - Lecture 8 Arrays and ArrayLists Arrays-1 Arrays are a simple data structure Arrays store a set of values of the same type Built-in types (int, double, etc.) or Objects (Students, Dates, etc.)
More informationCS 222: Pointers and Manual Memory Management
CS 222: Pointers and Manual Memory Management Chris Kauffman Week 4-1 Logistics Reading Ch 8 (pointers) Review 6-7 as well Exam 1 Back Today Get it in class or during office hours later HW 3 due tonight
More informationIntro to Computer Science II
Intro to Computer Science II CS112-2012S-05 I/O and ArrayList David Galles Department of Computer Science University of San Francisco 05-0: More on I/O Lots of ways to use Scanner class Always get more
More information01. Which of the following statement describes dynamic resizing as is applies to the ArrayList class?
Exposure Java Chapter 11 Multiple Choice Test ArrayList Class DO NOT WRITE ON THIS TEST This test includes program segments, which are not complete programs. Answer such questions with the assumption that
More informationCS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations
CS162: Introduction to Computer Science II Primitive Types Java Fundamentals 1 2 Primitive types The eight primitive types in Java Primitive types: byte, short, int, long, float, double, char, boolean
More informationGenerics. IRS W-9 Form
Generics IRS W-9 Form Generics Generic class and methods. BNF notation Syntax Non-parametrized class: < class declaration > ::= "class" < identifier > ["extends" < type >] ["implements" < type list >]
More informationgeneric programming alberto ferrari university of parma
generic programming alberto ferrari university of parma contents generic programming java generic programming methods & generic programming classes & generic programming java with generics generic methods
More informationCITS1001 week 4 Grouping objects
CITS1001 week 4 Grouping objects Arran Stewart March 20, 2018 1 / 31 Overview In this lecture, we look at how can group objects together into collections. Main concepts: The ArrayList collection Processing
More informationLearning 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
Learning objec-ves 2D Arrays (Savitch, Chapter 7.5) TOPICS Using 2D arrays Decomposi-on of a solu-on into objects and methods Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques () Lecture 20 February 28, 2018 Transition to Java Announcements HW05: GUI programming Due: THURSDAY!! at 11:59:59pm Lots of TA office hours today Thursday See Piazza
More informationTwo Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.
Class #07: Java Primitives Software Design I (CS 120): M. Allen, 13 Sep. 2018 Two Types of Types So far, we have mainly been dealing with objects, like DrawingGizmo, Window, Triangle, that are: 1. Specified
More informationChapter Seven: Arrays and Array Lists. Chapter Goals
Chapter Seven: Arrays and Array Lists Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the generalized for loop To study common array algorithms
More informationWorking with arrays. ArrayLists. Abstraction. Arrays
Working with arrays ArrayLists COMP4 Week 7 Common actions we want to do with arrays: adding and deleting copying looking for a particular element counting the elements Arrays Arrays are intrinsically
More informationJava Identifiers, Data Types & Variables
Java Identifiers, Data Types & Variables 1. Java Identifiers: Identifiers are name given to a class, variable or a method. public class TestingShastra { //TestingShastra is an identifier for class char
More informationCMSC 341. Nilanjan Banerjee
CMSC 341 Nilanjan Banerjee http://www.csee.umbc.edu/~nilanb/teaching/341/ Announcements Just when you thought Shawn was going to teach this course! On a serious note: register on Piazza I like my classes
More informationJAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015
JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -Lab 0 posted -getting started with Eclipse -Java refresher -this will not count towards your grade -TA office
More informationJump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.
Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:
More informationBuilding Java Programs
Building Java Programs Chapter 10 ArrayList reading: 10.1 Welcome to CSE 143! 2 Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[]
More informationComputational Expression
Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name
More informationBinghamton University. CS-140 Fall Interfaces
Interfaces 1 What can you do with a list? Java has a very strict definition every List in Java must support size() to get the number of items in a list get(n) to get the value of the n th element in the
More informationObjects and Iterators
Objects and Iterators Can We Have Data Structures With Generic Types? What s in a Bag? All our implementations of collections so far allowed for one data type for the entire collection To accommodate a
More informationswitch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++
Comp Sci 1570 Introduction to C++ Outline 1 Outline 1 Outline 1 switch ( e x p r e s s i o n ) { case c o n s t a n t 1 : group of statements 1; break ; case c o n s t a n t 2 : group of statements 2;
More informationFOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;
CLASSROOM SESSION Loops in C Loops are used to repeat the execution of statement or blocks There are two types of loops 1.Entry Controlled For and While 2. Exit Controlled Do while FOR Loop FOR Loop has
More informationArrays. CSE 142, Summer 2002 Computer Programming 1.
Arrays CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ 5-Aug-2002 cse142-16-arrays 2002 University of Washington 1 Reading Readings and References»
More informationJump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.
Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:
More information