Intro to Collections.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
Array Lists Chapter 7.2 Pages Array Lists In Java, Arrays are an important structure for storing data. We will learn more about arrays later,
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
DREW ALVAREZ AND CORDIE GOODRICH ARRAYS AND ARRAY LISTS.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
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.
Lists What to do?. Lists  A list is a linear arrangement of data elements.  Items are arranged in sequential (linear) order  Items are therefore ordered.
COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University
The ArrayList Data Structure Standard Arrays at High Speed!
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Exposure Java 2011 APCS Edition
CMSC 202 ArrayList Aug 9, 2007.
Chapter 6 – Array and Array Lists Two-Dimensional Arrays
Chapter 7 – Arrays and Array Lists
Sixth Lecture ArrayList Abstract Class and Interface
Array Lists An array list stores a sequence of values whose size can change. An array list can grow and shrink as needed. ArrayList class supplies methods.
Comp1004: Loops and Arrays II
(like an array on steroids)
Lecture 3 Linear Search and Binary Search ArrayLists
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Data Structures Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective.
ARRAYLIST AND VECTOR.
Arrays and the ArrayList Class The ArrayList Class
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
HW-6 Deadline Extended to April 27th
Welcome to CSE 143!.
Array List Pepper.
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Can store many of the same kind of data together
ArrayLists.
CS Week 9 Jim Williams, PhD.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Chapter 8 Slides from GaddisText
Arrays versus ArrayList
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CMSC 202 ArrayList Aug 9, 2007.
Chapter 10 ArrayList reading: 10.1
ArrayLists.
Welcome to CSE 143!.
Can store many of the same kind of data together
Arrays and Collections
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
Methods Copying Autoboxing
ArrayLists 22-Feb-19.
Can store many of the same kind of data together
Dr. Sampath Jayarathna Cal Poly Pomona
ArrayLists 27-Apr-19.
Creating and Modifying Text part 3
Review: libraries and packages
Arrays and ArrayLists.
Arrays.
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

Intro to Collections

Array Problems (Immediate) Must know size Hard to Grow Hard to Shrink Hard to insert in middle Hard to remove from middle Manual search

Array Problems (Future) Assumes Linear Relationship Right and left neighbors

Collections Hold a group of things (like a bag) Expect that all of the things are the same kind of thing (no flour in the sugar bag) Built in: Grow Shrink Insert Remove Search Variety of data structures

Collection Problems Additional overhead Does not quite store primitives Use wrapper classes

ArrayList Do not try to use to hold "large" numbers of primatives Simplest Collection Similar to array (linear) Automatically grows, shrinks, inserts, removes, searches Sort available Do not try to use to hold "large" numbers of primatives

ArrayList Must import import java.util.ArrayList; OR

Create an ArrayList New operator creates ArrayList object ArrayList<String> names = new ArrayList<String>(); “Generic” specifies what kind of thing can go in the collection Use generic in declaration and for the new operator (types should match) New operator creates ArrayList object

Add item to ArrayList names.add(name); //adds at end names.add(i, name); //adds in middle names.add(0, name); //adds at beginning

Remove item from Array list names.remove(i); //removes ith item

Let’s you use an ArrayList like an array Get and Set an Item name = names.get(i); names.set(i, “Bruce”); Let’s you use an ArrayList like an array

boolean found = names.contain(“Bruce”); Find an Item boolean found = names.contain(“Bruce”);

ArrayList Size int numOfNames = names.size(); Size of Array List grows and shrinks automatically as items are added and removed

Print an ArrayList System.out.println(names); Handy but may not be the format you want

Sort a Collection Collections.sort(names); Note to use sort items in collection must know how to compare themselves

Wrapper Classes Danger! Each primitive (int, double, boolean, etc.) has a corresponding wrapper class int -> Integer double -> Double boolean -> Boolean Used when Java needs an object Most of the time Java will switch from primitives to wrapper objects “as needed” this is called “autoboxing”. Danger!

For Each Another kind of for loop Visits each item in a collection “in order” Can also be used with array’s for(String name : names) { System.out.println(name); }