Arrays and Collections

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 Arrays.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
Introduction to arrays. Array Homogeneous collection of components stored in adjacent memory locations –All elements share same data type –Entire collection.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
The Java Collections Framework (JCF) Introduction and review 1.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Chapter 11 Arrays Continued
Final Exam Review Closed book Closed laptop One sheet of notes permitted SE-0010 Dr. Mark L. Hornick 1.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Chapter 8: Understanding Collections Textbook: Chapter 4.
More on Arrays Review of Arrays of ints, doubles, chars
Using the Java Collection Libraries COMP 103 # T2
Chapter 7 – Arrays and Array Lists
Sixth Lecture ArrayList Abstract Class and Interface
Hashing & HashMaps CS-2851 Dr. Mark L. Hornick.
Introduction to Linked Lists
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Array, Strings and Vectors
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 20 Generic Classes and Methods
Introduction to Linked Lists
Welcome to CSE 143!.
CMSC 341 Lecture 5 Stacks, Queues
Java Collections Overview
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
CS313D: Advanced Programming Language
Basic Files + (Chapter 10 – Arrays) : Objectives
Programming in Java Lecture 11: ArrayList
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.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
ArrayLists.
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 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Arrays and Collections
Welcome to CSE 143!.
Object-Oriented Programming Paradigm
Object Oriented Programming in java
Welcome to CSE 143! Go to pollev.com/cse143.
int [] scores = new int [10];
ArrayLists 22-Feb-19.
Collections Framework
Collections and iterators
Introduction to Data Structure
Review of Previous Lesson
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Java Programming Language
Review: libraries and packages
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Collections and iterators
Presentation transcript:

Arrays and Collections Intro to the Java Collections Framework Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick

Modern programming languages support the concept of collections That is, mechanisms to sort and manipulate many instances of the same type of data In Java, an array is an indexed collection of data values of the same type Arrays of both primitives or objects are possible Integer arrays String arrays WinPlotter arrays Winter 2004 CS-1010 Dr. Mark L. Hornick

Using constants to declare the array sizes does not always lead to efficient use of space rainfall = new double [12]; Declaration of arrays with constants is called fixed-size array declaration. Fixed-size array declaration may pose two problems; either: Not enough capacity for the task at hand. Wasted space (too much capacity) Winter 2004 CS-1010 Dr. Mark L. Hornick

Java is not limited to fixed-size array declaration int size; int[] number; size= Integer.parseInt( JOptionPane.showInputDialog(null, “Enter the size of an array:”)); number = new int[size]; // size determined from user input Winter 2004 CS-1010 Dr. Mark L. Hornick

Any valid integer arithmetic expression is allowed for specifying the size of an array size = Integer.parseInt( JOptionPane.showInputDialog(null,””)); number = new int[size*size + 2* size + 5]; Winter 2004 CS-1010 Dr. Mark L. Hornick

Object Deletion A Before is executed After is executed 1 2 3 person 1 CS-1020 12/29/2018 Object Deletion int delIdx = 1; person[delIdx] = null; Delete Person B by setting the reference in position 1 to null. A 1 2 3 person A B C D 1 2 3 person A C D Before is executed After is executed In this approach, we simply leave the position of a deleted object to a null. With this approach, an array index positions will be a mixture of null and real pointers. Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick

Java Collections Framework CS-1020 12/29/2018 Java Collections Framework The java.util standard package contains other types of classes for maintaining a collection of objects. We can add to, remove from, and retrieve objects in a collection. A collection does not have a set limit to the number of objects we can add to it. JCF includes classes that maintain collections of objects as sets, lists, or maps. CS2851 is an entire course based on JCF Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick

Advantages to using JCF collection classes Internal structure is hidden from view Code is optimized for performance Code is tested for quality More flexible Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick

CS-1020 12/29/2018 ArrayList is a collection class that implements the following methods (and others) boolean add( Object o ) Adds an object o to the list void clear( ) Clears this list, i.e., make the list empty Object get( int idx ) Returns the element at position idx boolean remove( int idx ) Removes the element at position idx int size() Returns the number of elements in the list There are total of 25 methods defined in the List interface. The five listed here are the very frequently used subset of those 25 methods. Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick

Generic ArrayList<E> ArrayList<Double> salaryList = new ArrayList<Double>(); E specifies the specific type of data that the ArrayList can manage You must substitute a class name for E; you cannot create an ArrayList of primitives This creates an instance, salaryList, of the ArrayList collection class. The elements in salaryList must be (references to) Doubles. Double is the wrapper class for the primitive type double. Generics allow you to create a class template that can use different types of objects when instantiated CS-2851 Dr. Mark L. Hornick

Type conversion is done automatically through boxing and unboxing Example: What if you want to insert a double value into an ArrayList<Double> at index 30? salaryList.add(30, 40000.00); This is called boxing: The automatic conversion of a primitive (double) value to the appropriate (Double) wrapper object To retrieve the value, no explicit cast is needed: double pay = /*(double)*/ salaryList.get(30); This is because although the get() method returns a Double, a Double can be converted to a double primitive type. This is called unboxing. CS-2851 Dr. Mark L. Hornick

ArrayList<E> usage features What if salaryList needs to be expanded? Done automatically! What if you need to know the number of elements currently in salaryList? salaryList.size() What if you want to insert the element at index 30? salaryList.add (30, 40000.00); // note: old element 30 becomes element 31, 31->32, etc To retrieve the value, no cast is needed: double sum = 0; sum = sum + salaryList.get(30); …provided that you use the generic version of ArrayList Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick

Built-in automatic sorting Suppose you randomly add elements to an ArrayList You can use the static sort() method of the Collections class to sort them Collections.sort(list); // list is an ArrayList Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick