CS 200 - Week 9 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Advertisements

CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
ArrayList, Multidimensional Arrays
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
CSE 1201 Object Oriented Programming ArrayList 1.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
CS 106A, Lecture 27 Final Exam Review 1
More on Arrays Review of Arrays of ints, doubles, chars
CMSC 202 ArrayList Aug 9, 2007.
Basic arrays, binary search, selection sort by amelia bateman
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 20: Wrapper Classes and More Loops
Arrays (review) CSE 2011 Winter May 2018.
(like an array on steroids)
Lecture 3 Linear Search and Binary Search ArrayLists
CompSci 230 S Programming Techniques
Chapter 7 Single-Dimensional Arrays
CS Week 14 Jim Williams, PhD.
HW-6 Deadline Extended to April 27th
CS 200 Arrays, Loops and Methods
CS 302 Week 15 Jim Williams, PhD.
Building Java Programs
Advanced Programming Behnam Hatami Fall 2017.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS Week 8 Jim Williams, PhD.
CS 200 Using Objects Jim Williams, PhD.
CS Week 6 Jim Williams, PhD.
CS Week 7 Jim Williams, PhD.
CS 106A, Lecture 19 ArrayLists
CS 200 Arrays, Loops and Methods
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
CS 302 Week 8 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
CS 200 Objects and ArrayList
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CMSC 202 ArrayList Aug 9, 2007.
Chapter 10 ArrayList reading: 10.1
ArrayLists.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
CS 200 More Primitives, Objects, Branches and Methods
Lecture 2: Implementing ArrayIntList reading:
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
CS 200 Loops Jim Williams, PhD.
CMSC 202 ArrayList Aug 9, 2007.
CS 200 Primitives and Expressions
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CS 200 Arrays Jim Williams, PhD.
slides created by Ethan Apter
CS 200 Primitives and Expressions
Lecture 13: ArrayLists AP Computer Science Principles
CS 200 Objects and ArrayList
Introduction to Data Structure
Review of Previous Lesson
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Building Java Programs
slides created by Alyssa Harding
CSE 143 Lecture 2 ArrayIntList, binary search
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Ethan Apter
Two-Dimensional Arrays
CS 200 Objects and ArrayList
Module 8 – Searching & Sorting Algorithms
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

CS 200 - Week 9 Jim Williams, PhD

This Week BP1: Milestone 2 due this week Scanner examples labs 3, 4, 6, and 8 Team Lab: Multi-Dimensional Arrays Bring paper and pencil to draw diagrams. Lecture: Objects and ArrayList comparison with arrays

What happens when array grows automatically array index out of bounds error who knows? You try to add more elements than will fit into an array? B and technically C since B is an error. (try it)

What happens when int [] list = new int[]{1,4,5}; list[1] = 2; list array now contains 1,2,5 list array now contains 1,2,4,5 Error int [] list = new int[]{1,4,5}; list[1] = 2; A is the best answer.

How do we insert 2 into the array? int [] list = new int[]{1,4,5}; list[1] = 2; //?

Recall Wrapper Classes Primitive Data Type Wrapper class int Integer double Double char 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? true Integer m = 5; Integer n = 5; false boolean result = m == n; true false error A is correct. The fine print at the provided link says for Integer instances based on int literals between -128 and 127 will have the same reference for the same int literal value. The intention of this example is to emphasize experimenting, discovering short illustrative examples and an introduction to the Java Language Specification. Part of relevant details: If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b. Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rule above is a pragmatic compromise, requiring that certain common values always be boxed into indistinguishable objects. Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html

Printing out, toString method Integer [] list2 = {1,2,3}; System.out.println( list2[1] ); System.out.println( list2 ); //java.util.Arrays.toString( list2)

Object Class java.lang.Object has methods such as toString()

Arrays vs ArrayList Arrays fixed length, contiguous in memory automatically grows as elements are added by allocating new array and copying.

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

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

Lots of Elements ArrayList<Integer> list4; list4 = new ArrayList<Integer>(); for ( int i = 0; i < 1000; i++) { list4.add( i); } System.out.println( list4);

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 have to make sure there is enough room and move all the elements down and then insert it. Insertion is handled automatically You want to insert an element in an ArrayList at a specific index? B is the best answer

How many elements in an Array vs ArrayList? static void methodA(int [] arr) { //how to find capacity of array? } static void methodB(ArrayList list) { //how to find the number of //elements in list? arr.length list.size() list.length() arr.size correct: A (try it)

What size and elements? 5 [A,B,C,D,E] 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 } public static void printArray(int [] arr) { System.out.print("\n["); boolean first = true; for ( int num : arr) { if ( first) { first = false; } else { System.out.print(", "); } System.out.print( num); System.out.println("]");

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) { } /** * Returns the index of where the element x was found * or -1 if not found. * @param arr * @param x * @return */ public static int linearSearch( int arr[], int x) { for ( int i = 0; i < arr.length; i++) { if ( arr[i] == x) { return i; } return -1;

Binary Search // Return index of where x is in list if found, // otherwise returns -1; public static int binarySearch(ArrayList<Integer> list, int x) { } /* * Return index of where x is in array if found, otherwise * returns -1; */ public static int binarySearch(int arr[], int x) { int left = 0, right = arr.length - 1; while (left <= right) { //determine mid point int mid = left + (right-left)/2; //check mid point if (arr[mid] == x) return mid; //if x is greater than mid point then change left if (arr[mid] < x) left = mid + 1; //if x is less than mid point then change right else right = mid - 1; } //not found, return -1 return -1;

Draw Picture ArrayList<ArrayList<String>> list = new ArrayList<>(); 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

Sorting public static void bubbleSort( ArrayList<Integer> list) { } public static void bubbleSort(ArrayList<Integer> list) { int temp = 0; for (int i = 0; i < list.size(); i++) { for (int j = 1; j < (list.size() - i); j++) { if (list.get(j - 1) > list.get(j)) { // swap temp = list.get(j - 1); list.set(j - 1, list.get(j)); list.set(j, temp); }