CS 200 Objects and ArrayList Jim Williams, PhD

Similar documents
AP CS Unit 7: Interfaces Exercises Assume all code compiles unless otherwise suggested.

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

Array Based Lists. Collections

CS 106A, Lecture 19 ArrayLists

CS 200 Arrays, Loops and Methods Jim Williams, PhD

CS Week 14. Jim Williams, PhD

CS Week 13. Jim Williams, PhD

After the code has executed, what is the capacity of list? What is its size? c

CS 200 More Classes Jim Williams, PhD

All answers will be posted on web site, and most will be reviewed in class.

CS Week 5. Jim Williams, PhD

CS Programming I: ArrayList

Building Java Programs

CS 302 Week 14. Jim Williams, PhD

CS 302 Week 9. Jim Williams

Building Java Programs

CS 307 Midterm 2 Spring 2008

CS 200 Using Objects. Jim Williams, PhD

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

CS 302: Introduction to Programming in Java. Lecture 12

1.00 Tutorial 3. Methods, Classes Arrays & ArrayLists. September 26 & 27, 2005

public static<e> List<E> removeoccurrences(list<e> origlist, E remove) {

Using arrays to store data

CS 106A, Lecture 20 ArrayLists and HashMaps

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

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Building Java Programs

AP Computer Science. ArrayLists

Arrays and ArrayLists. David Greenstein Monta Vista High School

AP CS Unit 7: Interfaces. Programs

1. Write the output generated by the following code. (8pts) 2. Write the output generated by the following code (8pts)

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

Review: Array Initializer Lists

CSE 143 Lecture 4. ArrayList. Reading: slides created by Marty Stepp

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

CS 312 Final Fall 2016

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

CS 314 Exam 1 Spring 2017

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

AP Computer Science Midterm Review Part 1

COE318 Lecture Notes Week 6 (Oct 10, 2011)

CITS1001 week 4 Grouping objects

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

PIC 20A Number, Autoboxing, and Unboxing

CSCI 355 Lab #2 Spring 2007

CS 314 Exam 1 Fall 2017

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

Welcome to the Using Objects lab!

Week 6 CS 302. Jim Williams, PhD

CSCI 355 LAB #2 Spring 2004

Computational Expression

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

Generics. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 10

Flowcharts [15 points]

ECE 122. Engineering Problem Solving with Java

CMSC 202. Containers

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

CS 106A, Lecture 27 Final Exam Review 1

Recitation 3/27/2009. CS 180 Department of Computer Science, Purdue University

You must bring your ID to the exam.

Introduction to Computer Science I

Building Java Programs

Advanced Java Concepts Unit 2: Linked Lists.

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CS1083 Week 2: Arrays, ArrayList

CS 307 Midterm 2 Fall 2009

CS 307 Final Spring 2008

CS Week 2. Jim Williams, PhD

Language Features. 1. The primitive types int, double, and boolean are part of the AP

ArrayLists. COMP1400 Week 8. Wednesday, 12 September 12

Intro to Computer Science II

1. ArrayList and Iterator in Java

Review of Java. or maybe not

CSCI 1103: Array-based Data Structures

CS 211: Using ArrayList, Implementing Arraylist

Exercise. Write a program that reads a file and displays the words of that file as a list.

Points off Total off Net Score. CS 314 Final Exam Spring 2016

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Algorithms. Produced by. Eamonn de Leastar

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

Big O & ArrayList Fall 2018 Margaret Reid-Miller

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

ArrayLists. Chapter 12.1 in Savitch

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

CSE 143 Lecture 4. Preconditions

CS Week 2. Jim Williams

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

Lecture Multidimensional Arrays and the ArrayList Class. Dr. Martin O Connor CA166

CompSci 125 Lecture 11

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

1.00/ Lecture 8. Arrays-1

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CSC 204 Lab 12: Intro to ArrayLists

CS 200 File Input and Output Jim Williams, PhD

CSE 143. Computer Programming II

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

2. The object-oriented paradigm

Working with arrays. ArrayLists. Abstraction. Arrays

Write the letter of the correct answer for each problem in the blanks on the answer form.

Transcription:

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 Standards 4. Lecture: Objects and ArrayList a. comparison with arrays

Object class java.lang.object Every class is a descendent of Object, either directly or indirectly. has methods such as tostring() that are inherited by every class.

class vs instance/object class: Object,String,Random,Scanner,Integer instance/object: new String("hello") new Integer(3) new Random() new Scanner( System.in)

Recall Wrapper Classes Primitive Data Type Wrapper class int double char Integer Double Character etc. Boxing: create an instance of wrapper class for primitive value. Unboxing: get primitive value from instance of wrapper class. Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html

Is result true or false? Integer m = 5; Integer n = 5; boolean result = m == n; true false error Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html

Printing out, tostring method Integer [] list = {1,2,3}; System.out.println( list[1] ); System.out.println( list ); //java.util.arrays.tostring( list)

How do we add 7 to the array? int [] list = new int[]{1,4,5}; list[3] = 7; //?

Arrays vs ArrayList Arrays fixed length, constant access time works with any type ArrayList fixed length, constant access time only works with Reference types automatically grows as elements are added by allocating a new array and copying.

ArrayList Diagram ArrayList<Integer> list; list = new ArrayList<Integer>(); list.add( 2); list.add(0,3); System.out.println( list);

Draw a Picture ArrayList<Integer> list; list = new ArrayList<Integer>(20); list.add(0,3); list.add(5); list.add(0,4); list.remove( 1); System.out.println( list);

How many elements? ArrayList<Integer> list4; list4 = new ArrayList<Integer>(100); for ( int i = 0; i < 1000; i++) { list4.add( i); } System.out.println( list4.size()); 1000 100 error (after 100 added)

Java Source Code Usually within Java Development Kit (JDK). On Windows typically found under: C:\Program Files\Java Look for src.zip

Capacity vs Size Array Capacity.length attribute Size keep separate count of elements inserted in array ArrayList changes as needed.size() method

How to retrieve the number of elements added to an Array vs ArrayList? static int numelements(int [] arr) { return A; } static int numelements(arraylist list) { return B; } A arr.length B list.size() A arr.length B list.length() A arr.size B list.length() A arr.size B list.size() Other

Adding an array to ArrayList ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); Integer [] arr = new Integer[]{1,3,4,5}; list.addall( java.util.arrays.aslist( arr)); System.out.println( list);

What happens when You want to insert an element in an ArrayList at a specific index? you have to make sure there is enough room and move all the elements down and then insert it. Insertion is handled automatically

What size and elements? ArrayList<String> list; list = new ArrayList<String>(); list.add("a"); list.add(0,"b"); list.add("c"); list.set(2,"d"); list.add("e"); System.out.println( list.size()); System.out.println( list); 5 [A,B,C,D,E] 4 [B, A, D, E] 3 [B,D,E] error or other

Enhanced For Loop ArrayList<String> names = new ArrayList<>(); names.add("spot"); names.add("fido"); for ( String name : names) { System.out.println( name); }

Write a method to print out array public static void printarray(int [] arr) { //use enhanced for loop }

Linear Search //Returns the index of where the element x was //found or -1 if not found. public static int linearsearch( ArrayList<Integer> list, int x) { }

Binary Search // Return index of where x is in list if found, // otherwise returns -1; public static int binarysearch(arraylist<integer> list, int x) { }

Draw Picture ArrayList<ArrayList<String>> list = new ArrayList<>(); list.add( new ArrayList<String>()); list.add( new ArrayList<String>()); list.add(1, new ArrayList<String>()); list.get(1).add("d"); list.set(0, list.get(1)); list.get(2).clear(); System.out.println( list); [[],[],[]] [[], [D], []] [[D],[D],[]] error or other