Arrays and ArrayLists. David Greenstein Monta Vista High School

Similar documents
The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

Big O & ArrayList Fall 2018 Margaret Reid-Miller

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

CS 302: Introduction to Programming in Java. Lecture 12

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Chapter 9 Introduction to Arrays. Fundamentals of Java

Stacks and Queues. David Greenstein Monta Vista

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Objektorienterad programmering

Introduction to Programming Using Java (98-388)

List ADT. B/W Confirming Pages

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Lecture 6: ArrayList Implementation

CS 211: Using ArrayList, Implementing Arraylist

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

COMP More About Arrays. Yi Hong June 05, 2015

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Arrays. Eng. Mohammed Abdualal

Array. Prepared By - Rifat Shahriyar

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

FORM 2 (Please put your name and form # on the scantron!!!!)

Linked List. ape hen dog cat fox. tail. head. count 5

Object-Oriented Design and Programming (Java)

Arrays.

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Introduction to Object-Oriented Programming

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

Chapter 7: Arrays CS 121. April 9, Department of Computer Science College of Engineering Boise State University. Chapter 7: Arrays CS / 41

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CS Programming I: ArrayList

1.00/ Lecture 8. Arrays-1

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

array Indexed same type

Array Based Lists. Collections

Principles of Programming. Chapter 6: Arrays

CS 211: Using ArrayList, Implementing Arraylist

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

CS 177 Week 15 Recitation Slides. Review

CS 307 Midterm 2 Spring 2008

You must bring your ID to the exam.

TeenCoder : Java Programming (ISBN )

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

CS 230 Programming Languages

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

Last Class. More on loops break continue A bit on arrays

Example: Computing prime numbers

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

CSC 222: Object-Oriented Programming. Spring 2012

CS 200 Objects and ArrayList Jim Williams, PhD

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

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

AP Computer Science Lists The Array type

Lists using ArrayList

AP Programming - Chapter 13 Lecture page 1 of 17

Slides are adapted from the originals available at

ArrayList<String> listname; //sets aside memory to store a reference //nothing has been constructed List<Integer> numberlistname;

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

CS 231 Data Structures and Algorithms, Fall 2016

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

Test 6. moodle.yorku.ca CSE 1020

Lecture 13: Two- Dimensional Arrays

CS Introduction to Data Structures Week 1 Thursday

COMP 202. Programming With Arrays

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

STUDENT LESSON A15 ArrayList

Chapter 7: Arrays and the ArrayList Class

Computer Science 210 Data Structures Siena College Fall 2018

CSC 1052 Algorithms & Data Structures II: Lists

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Generics. IRS W-9 Form

AP Computer Science Midterm Review Part 1

CompuScholar, Inc. 9th - 12th grades

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

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

Use of the ArrayList class

Java Collections Framework: Interfaces

Programming Syntax and Style. David Greenstein Monta Vista High School

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

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

The Warhol Language Reference Manual

Data & Procedure Introduction

Arrays and ArrayLists. Ananda Gunawardena

Arrays in Java Multi-dimensional Arrays

Here is how we use an arraylist. To access the arraylist code we can import the class via:

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Collections

Using arrays to store data

Lecture 5: Arrays. A way to organize data. MIT AITI April 9th, 2005

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

CS201- Introduction to Programming Current Quizzes

Transcription:

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 elements. When we say element we often mean the value stored in that element. double [] arr = new double [7]; Consecutive memory locations holding doubles Memory 000FE 1.00349 000FF 34.5 00100 3.3 00101 83765.1 00102 98.231 00103 0.0 00104 0.0

Array (cont) Rather than treating each element as a separate named variable, the whole array gets one name. Specific array elements are referred to by using the array s name and the element s number, called the index or subscript. double [] arr = new double [7]; Array arr and index arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] Memory 000FE 1.00349 000FF 34.5 00100-3.3 00101 83765.1 00102 8.231e2 00103 0.0 00104 0.0

Indices/Subscripts Indices can be any int value represented by a literal number, an int variable, or an expression that evaluates to an int value. The range of valid indices start at the first element indicated by a zero (0), to the last element indicated by array s length minus 1 (arr.length - 1). arr[2] arr[i] int literal int variable arr[(int)(math.random() * 10)] int expression

Indices/Subscripts (cont) In Java, an array is initialized with fixed length that cannot be changed. The Java interpreter checks the values of indices at run time and throws ArrayIndexOutOfBoundsException if an index is negative or if it is greater than the array length - 1 (eg. arr.length - 1). arr[2] arr[i] int literal int variable arr[(int)(math.random() * 10)] int expression

Power of Arrays Arrays allow us to gather similar information together into a list. Most programming languages have arrays with indices. Indices give direct access to each element quickly. Indices can be computed during runtime to help with repeating similar operations over the list. 1000 lines!! Without Arrays int sum = 0; sum += score0; sum += score1; sum += score999; With Arrays sum = 0; for(int a = 0; a < scores.length; a++) sum += scores[a];

Arrays are Objects An array in Java is an object without methods. Therefore, there is no class that describes an array. (Remember, array.length is a property!) An array is created and memory is allocated using the new operator (just like other objects!). An array must be declared and created before it can be initialized. Declare, Create, Initialize int [] arr; arr = new int [10]; for (int a = 0; a < arr.length; a++) arr[a] = a; Declare and create String [] strarr = new String [100]; strarr[0] = Alice ; Initialize

Initializing Arrays Arrays can contain either primitives or objects. Once an array is created it, each element contains the same default values as a field of a class: numbers are zero boolean are false char is the null character objects are null pointers. int array contains 0 s String array contains null pointers int [] arr; arr = new int [10]; String [] strarr = new String [100];

Initializing Arrays (cont) Arrays can be declared, created, and initialized in the same statement. Optional int [] nums = new int[] { 1, 2, 3, 4 }; String [] strnums = new String [] { one, two, three, four, five }; If you want to create an array on-the-fly, always use new datatype [ ] Required nums = new int[] { 1, 2, 3, 4 }; rendertokens(new String [] { <html>, <body>, <p>, hello, </p>, } );

Arrays are Mutable You can change an element in an array and all references to the array are also changed. Pass array into method String[] arr = new String[10]; arr[5] = Hello ; System.out.print(arr[5]); mymethod(arr); System.out.print(arr[5]); Prints Hello Prints Goodbye public void mymethod(string[] x){ x[5] = Goodbye ; }

Array Length The length of an array is determined when it is created, either by the contents of braces {} or a number explicitly in brackets. (ie. [10]) char[] letters = { a, m, s, z }; String[] names = new String[10]; In Java, the length of an array is a property and not a method. char[] arr = new char[10]; int len = arr.length; Correct char[] arr = new char[10]; int len = arr.length(); Syntax Error!!

Passing Arrays to Methods As other objects, an array is passed to a method as a reference. (pass-by-reference) The elements of the original array are not copied and are accessible in the method s code. Before method call Method After method call int[] arr = { 1, 2, 3, 4 }; shiftright(arr); public void shiftright(int[] nums) { int last = nums[nums.length-1]; for (int a = nums.length-1; a > 0; a ) nums[a] = nums[a-1]; nums[0] = last; } arr = { 4, 1, 2, 3 }

Returning Arrays from Methods Sometimes you want a method to return a new array. /* Calculate the midpoint between two points. * Return a coordinate pair in an array. */ public double[] midpoint(double x1, double y1, double x2, double y2) { return new double[] { (x1 + x2)/2, (y1 + y2)/2 }; } Sometimes you want to use an array as a parameter, return a new array, but keep the original untouched. /* Returns a copy of an int array. */ public int[] copyint(int[] inarr) { int[] result = new int[inarr.length]; for (int a = 0; a < inarr.length; a++) result[a] = inarr[a]; return result; }

Two-dimensional Arrays Two-dimensional arrays allow us to represent 2D figures like tables, grids, game boards, images, etc. An element in a two-dimensional array is accessed using a row index and column index. For example: table[2][3] = secret ; columns 0 1 2 3 rows 0 1 2 secret 3 4

Two-dimensional Arrays Two-dimensional arrays allow us to represent 2D figures like tables, grids, game boards, images, etc. An element in a two-dimensional array is accessed using a row index and column index. For example: grid[0][0] rows grid[6][0] 0 1 2 3 4 5 6 grid[2][3] = X ; columns 0 1 2 3 4 5 X grid[0][5] grid[6][5]

Declaring 2-D Arrays // array with 5 rows and 7 columns double[][] arr = new double [5][7]; // 2D array containing objects Color [][] pixels = new Color[480][720]; // Declaring and initializing 2D array int [][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

2D Array Dimensions A two-dimensional array is really a 1-D array of 1-D arrays. int[][] arr = new int[2][3]; arr[k] is a 1-D array in the k-th row. arr.length is the number of rows. arr[k].length is the number of columns in row k. rows arr[0] arr[1] arr[2] arr[3] columns 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 arr[0][3] arr[2][1] arr[3][5]

2D Array Dimensions (cont) Java allows jagged arrays in which different rows have a different number of columns. (also called ragged array) In a rectangular array m[0].length is the number of columns in all rows. Jagged array Rectangular array columns 0 1 2 3 4 5 columns 0 1 2 3 4 5 0 0 rows 1 rows 1 2 2 3 3 4 4

2D Array Dimensions (cont) Creating and initializing jagged arrays is similar to 1D arrays. int[][] arr = new int[5][0]; arr[0] = new int[6]; arr[1] = new int[4]; arr[2] = new int[] { 0, 99, 2, 34, 55, 66 }; arr[3] = new int[5]; arr[4] = new int[] { 8, 13 }; Array arr[][] 0 rows 1 2 3 4 0 columns 1 2 3 4 5 0 99 2 34 55 66 8 13

2-D Arrays and Nested Loops To reach each element in a rectangular 2D array it is necessary to use nested loops. for (int r = 0; r < arr.length; r++) for (int c = 0; c < arr[0].length; c++) { // process arr[r][c] } Triangular loops are nested loops that use the row s value to determine the column s range of values. // transpose a square matrix for (int r = 1; r < arr.length; r++) for (int c = 0; c < r; c++) { double temp = arr[r][c]; arr[r][c] = arr[c][r]; arr[c][r] = temp; }

For Each Loop Introduced in Java version 5. (Lab computers are on version 7; the current version is 8) One-dimensional array example: double[] onedarr = new double[10]; for (double element : onedarr) { // process element } Two-dimensional array example: String[][] twodarr = new String[32][24]; for (String[] strarr : twodarr) for (String element : strarr) { // process element }

For Each Loop (cont) Best for doing identical processes on each element regardless of their position in the array. // get sum of elements in array int sum = 0; for (int element : anyarr) sum += element; Not good when you are doing operations for specific indices. // get sum of every other element int sum = 0; int cnt = 0; for (int element : anyarr) { if (cnt % 2 == 0) sum += element; Better for loop cnt++; } // get sum of every other element int sum = 0; for (int a = 0; a < anyarr.length; a++) if (a % 2 == 0) sum += anyarr[a];

ArrayLists

java.util.arraylist<e> ArrayList is a class in the java.util package. java.util.arraylist<e> The <E> stands for generic data type Element that will be in the ArrayList. For example: ArrayList<String> means the ArrayList contains String elements. ArrayList implements java.util.list<e> interface. (Chapter 19) «interface» java.util.list java.util.arraylist java.util.linkedlist

java.util.arraylist<e> (cont) ArrayList can hold only objects of a specified type <E>, but never primitive data types. ArrayList keeps track of the number of elements (called size). In Java, an ArrayList starts with a default capacity of 10 elements. Although ArrayList keeps track of the capacity of the list, Java does not share how it grows and shrinks with the programmer. Size Cat Hat Mat Capacity

ArrayList and Generics Starting with Java version 5, collection classes like ArrayList began holding objects of a specified data type. We use version 7 in the labs. The current version is 8, soon to be 9. A generic class, like ArrayList, forces it to hold only one data type so type checking can be done by the compiler. ArrayList<Integer> nums = new ArrayList<Integer>(); List<String> words = new ArrayList<String>(); «interface» java.util.list java.util.arraylist java.util.linkedlist

ArrayList and Generics (cont) A common problem when using generics in your code is the compiler unchecked or unsafe operations error. Suppose our code looked like this: No data type specified public class Snake extends ArrayList { } % javac Snake.java Note: Snake.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. public class Snake extends ArrayList<Coordinate> { } Fix

ArrayList Constructors ArrayList has two constructors. ArrayList<E>() Creates an empty ArrayList<E> with a default capacity (10) ArrayList<E>(int num) Creates an empty ArrayList<E> with a starting capacity of num

ArrayList Methods (abridged) int size() boolean isempty () boolean add (E obj) void add (int a, E obj) E set(int a, E obj) E get(int a) E remove(int a) boolean contains(e obj) int indexof(e obj) returns true inserts obj as the a-th element; a must be from 0 to size() a must be from 0 to size() - 1 both use E s equals() to compare objects

ArrayList<E> Details ArrayList automatically doubles its capacity when it needs more space. get(int a) and set(int a, E obj) are efficient because an array provides random access to its elements. It throws an IndexOutOfBoundsException when the index is less than 0 or equals size() or greater. Therefore, index a must be in the range 0 <= a < size(). In the case of add(int a, E obj), index a must be in the range 0 <= a <= size().

ArrayList<E> Autoboxing Normally, if you have primitives to add to an ArrayList you must use a wrapper class, like Integer or Double. ArrayList<Integer> intnums = new ArrayList<Integer>(); intnums.add(new Integer(5)); // add Integer(5) to list Since Java version 5, conversion from primitive to wrapper class object is automatic. For example, int 5 is converted into a new Integer(5). ArrayList<Integer> intnums = new ArrayList<Integer>(); intnums.add(5); // add Integer(5) to list This automatic conversion is called autoboxing or autowrapping.

ArrayList<E> Autounboxing Since Java 5, it also supports the opposite of autoboxing, called autounboxing. ArrayList<Integer> intnums = new ArrayList<Integer>(); intnums.add(new Integer(5)); // add Integer(5) to list int a = 97 + intnums.get(0); auto-converts Integer object to primitive int int a = 97 + intnums.get(0).intvalue();

ArrayList Blunders // remove all Hello strings?? for (int a = 0; a < words.size(); a++) if ( Hello.equals(words.get(a))) words.remove(a); Only removes one of two consecutive Hello s [ Hello, Hello, Goodbye ] [ Hello, Goodbye ] // remove all Hello strings for (int a = 0; a < words.size(); a++) if ( Hello.equals(words.get(a)) { words.remove(a); a- ; } // remove all Hello strings int a = 0; while (a < words.size()) if ( Hello.equals(words.get(a)) words.remove(a); else a++; Removes all Hello s in list correctly

ArrayList For Each Loop For each works with Lists including ArrayLists ArrayList<String> words = new ArrayList<String>(); for (String word : words) { // process word } Same As ArrayList<String> words = new ArrayList<String>(); for (int a = 0; a < words.size(); a++) { String word = words.get(a); // process word }

Questions?