Lesson 2.4 Arraylist

Similar documents
Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

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

ECE 122. Engineering Problem Solving with Java

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

STUDENT LESSON A15 ArrayList

Big O & ArrayList Fall 2018 Margaret Reid-Miller

CSE 142 Sp02 Final Exam Version A Page 1 of 14

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

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

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Garbage Collection (1)

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

ArrayList. Introduction. java.util.arraylist

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

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

QUIZ. What is wrong with this code that uses default arguments?

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

CS2110: Software Development Methods. Maps and Sets in Java

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

ArrayLists. Readings and References. Collections in the Real World. How can we manage lists of objects? Reading. Other References

ArrayLists. CSE 142, Summer 2002 Computer Programming 1.

Use of the ArrayList class

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

CS 170 Java Programming 1. Week 10: Loops and Arrays

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Exceptions in Java

Expressions and Casting

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

CS 251 Intermediate Programming Java Basics

Computer Science 210 Data Structures Siena College Fall 2018

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Lesson 26: ArrayList (W08D1)

+ Abstract Data Types

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

377 Student Guide to C++

Arrays in C++ Instructor: Andy Abreu

AP Computer Science Homework Set 1 Fundamentals

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

Control Structures in Java if-else and switch

Java Programming. Computer Science 112

What did we talk about last time? Course overview Policies Schedule

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

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

COMP 202. Programming With Arrays

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

CS Final Exam Review Suggestions - Fall 2017

CS Final Exam Review Suggestions - Spring 2014

EXAM Computer Science 1 Part 1

Java Programming. Computer Science 112

Inf1-OP. Collections. Perdita Stevens, adapting earlier version by Ewan Klein. January 9, School of Informatics

CS2110 Assignment 3 Inheritance and Trees, Summer 2008

CS2110: Software Development Methods. Maps and Sets in Java

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Lecture 11: Recursion (hard)

Lab5. Wooseok Kim

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Text Input and Conditionals

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

COMP 110/L Lecture 4. Kyle Dewey

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

4. Java language basics: Function. Minhaeng Lee

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Topic 4 Expressions and variables

[ the academy_of_code] Senior Beginners

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Exercise: Inventing Language

Lists. Chapter 12. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSE 143. Computer Programming II

Lesson 12: The Graph of the Equation y = f(x)

Introduction to Object-Oriented Modelling and UML

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

CSE 113 A. Announcements - Lab

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

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

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

The Java Collections Framework and Lists in Java Parts 1 & 2

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

ECE 122. Engineering Problem Solving with Java

REMINDER CS121/IS223. Agenda. A Recap on Arrays. Array of Cars for the Car Race? Arrays of Objects (i)

CS121/IS223. Collections Revisited. Dr Olly Gotel

The Big Python Guide

Arrays. myints = new int[15];

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page]

Tips from the experts: How to waste a lot of time on this assignment

CS558 Programming Languages

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Java Array List Interview Questions

CS61BL: Data Structures & Programming Methodology Summer 2014

Survey #2. Assignment #3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Static Interface Types.

CS61BL: Data Structures & Programming Methodology Summer 2014

Building Java Programs

Claremont McKenna College Computer Science

Transcription:

Lesson 24 Arraylist Mimi Duong Rosalba Rodriguez Java Crash Course January 6th, 2015

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Storing Items in Java How have we been storing our information recently? storing items inside instance variables, and calling on it when we need it for example: int x = 5; Systemoutprintln(x); // prints 5 So what happens if we have multiple items of the same type that we want to store? Say we want to have and use 15 integer values? Would we make 15 different variables, one at a time?

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Data Structures A data structure is a way to store multiple things of the same type in Java examples from real life: grocery lists, class schedule, schedule for the day, to do list, etc

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Data Structures Just as we had examples in real life of data structures, there are data structures in Java Although there are many different data structures in Java, right now we are only going to focus on ArrayLists

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork ArrayList Think of ArrayList as just a regular list The same thing you do to lists in real life, you can do to and ArrayList in Java add an item remove an item change the order of items *Remember that when you make an ArrayList in Java, you have to store the same type of items inside of this ArrayList In addition to this, know that this is a premade class inside of Java So similar to Scanners, we will have to import the class the file we are using ArrayLists in order to use it properly We do so by the following: import javautilarraylist;

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork ArrayList General syntax for declaring an ArrayList: ArrayList<TypeOfList> name = new ArrayList<TypeOfList>();

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Live Coding Follow along with the instructor on the creating ArrayLists!

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Live Coding Review Some things to take into consideration when using ArrayLists: Index of an ArrayList starts at 0 So if the size of your ArrayList is 1, then the index of that one object is 0! If the size of your ArrayList is 10, then the index of the last object is 9 and the index of the first object is 0 You need to import Arraylists before you can use them ArrayLists can be as big or as small as you want They are a type of a data structure (used to hold objects) An ArrayList will only hold the same object, where you tell Arraylist what it holds when you create and instantiate it

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Methods for ArrayList Just as we can do different things to lists that we have in real life, we can do similar things to the ArrayLists that we have in Java add(object i) - adds the object between the paranthesis into the end of the ArrayList remove(int i) - removes the object that is located at the index i inside the ArrayList size() - returns how many objects are inside of the ArrayList get(int i) - returns the object that is located at the index i inside the ArrayList contains(object i) - returns true or false depending on whether or not object i exists inside the ArrayList As always, you can find additional methods ArrayLists have to use inside the ArrayList API by CLICKING HERE

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Making the Program Look Through the ArrayList Why would we do this in the first place? The list is stored in the computer s memory we won t remember it all (imagine lists of thousands of objects) To print it onto the screen for whatever purposes is necessary ie such as a teacher printing out all the grades in a class To search through the list for a specific object in order to do something to it To see if there are any single object that exists in two specific ArrayLists

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Relating to for-loops This is where for-loops come in handy because We can loop through all the indexes for ArrayLists Starting at index 0, we can keep increasing the index until we get to the last object of the ArrayList! Follow along with the instructor on how to do so

Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Classwork 10 Classwork 10, otherwise known as Homework 2 Extra Credit Because the requests for this classwork is slightly wrong, please READ ABOUT IT HERE Make sure you download the SKELETON CODE HERE As always, break the problem into smaller pieces