Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
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.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays, part 2. Array applications Arrays are useful whenever a relatively large amount of data must be kept available in memory for processing We will.
Arrays.
©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 2D Arrays.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 Arrays.
Recitation 3/27/2009 CS 180 Department of Computer Science, Purdue University.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
CS180 Recitation 25th/26th October, 2007 Department of Computer Science, Purdue University.
©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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Chapter 9: Advanced Array Concepts
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
111 © 2002, Cisco Systems, Inc. All rights reserved.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
Chapter 9 Multidimensional Arrays and the ArrayList Class.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Chapter 18 Java Collections Framework
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Maps Nick Mouriski.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Collections Dwight Deugo Nesa Matic
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
JAVA COLLECTIONS LIBRARY
Chapter 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Arrays and Collections
Object Oriented Programming in java
Chapter 7 Part 2 Edited by JJ Shepherd
Arrays.
Presentation transcript:

Chapter 10 2D Arrays Collection Classes

Topics Arrays with more than one dimension Java Collections API ArrayList Map

Information Represented as a Table Step -> Grade

Two-Dimensional Arrays In Java, data may be organized in a two- dimensional array. –A table is an example of a two-dimensional array. In a two-dimensional array, two indices (in a table, one for the row and one for the column) are used to refer to the array element.

Two-Dimensional Arrays To declare our example array, we state: double[][] payScaleTable; double payScaleTable[][]; and create the array as payScaleTable = new double[4][5];

Indexing Two-Dimensional Arrays To refer to an element at the second column (column 1) of the third row (row 2), we say payScaleTable[2][1] Nested-for loops are useful for manipulating two-dimensional arrays.

Element Access for 2D Array

Two-Dimensional Arrays The concept of the two-dimensional array in Java is just that: a concept. There is no explicit structure called the “two- dimensional array” in Java. The two-dimensional array concept is implemented by using an array of arrays.

Two-Dimensional Arrays The sample array creation payScaleTable = new double[4][5]; is a shorthand for payScaleTable = new double [4][ ]; payScaleTable[0] = new double [5]; payScaleTable[1] = new double [5]; payScaleTable[2] = new double [5]; …

Instanitating a 2D Array

Instantiation, continued

Size of Two-Dimensional Arrays The expression payScaleTable.length refers to the length of the payScaleTable array itself.

Size of Two-Dimensional Arrays The expression payScaleTable[1].length refers to the length of the array stored at row 1 of payScaleTable.

Subarrays An array that is part of another array is called a subarray. An array of arrays may be initialized when it is created.

Ragged Arrays Subarrays may be different lengths. Executing triangularArray = new double[4][ ]; for (int i = 0; i < 4; i++) triangularArray[i] = new double [i + 1]; results in an array that looks like:

Lists and Maps The java.util library contains high-power classes for maintaining a collection of objects. These classes are collectively referred to as the Java Collection Framework (JCF).

Lists The List interface is one useful feature that can help us manage large numbers of objects. An interface defines the behavior of a class; a list of public methods without method bodies. We cannot create an instance of an interface.

Lists Two classes in the JCF implement the List interface: –ArrayList –LinkedList The ArrayList class uses an array to manage data. The LinkedList class uses a technique called linked-node representation.

Creating a List To use the List interface, we declare the variable as List and assign an instance of the class that implements the List interface to it: List myList;... myList = new ArrayList( ); This approach permits a quick change of the implementation class if necessary.

Lists The default constructor will create an empty list with an initial capacity of 10. It is possible to increase the capacity of a list. However, it is better to create a list with the actual capacity we need, if we know in advance what that capacity is.

List Methods The add method allows us to add objects to the list. The capacity method gives us the current capacity of a list. To find out the number of objects contained in a list, we use its size method.

List Methods The remove method takes an element’s index as its parameter and allows us to remove an element from a list. The get method allows us to access objects stored in a list by giving their index position in the list. The iterator method also allows us to scan the elements inside a list or other JCF collection classes.

List Iterators When we call a list’s iterator method, an Iterator object (an instance of a class that implements the Iterator interface) that supports two methods, hasNext and next, is returned. –hasNext returns true if the iterator has more elements to access. –next returns the next element in the list Calling next if there are no more elements to access will result in an error.

Lists The iterator method is supported by many other java.util classes. A list cannot include primitive data values as its elements, but wrapper classes can adapt the values to acceptable list objects.

Maps Two classes implement the Map interface: –HashMap –TreeMap TreeMap implements a subinterface of Map called SortedMap, where the entries in the map are sorted.

Maps A map consists of entries. Each entry is divided into two parts: –key –value Duplicate keys are not allowed in the map. Both the key and the value may be instances of any class.

Maps A map may be characterized as an expandable array with instances of any class as its indices. The main advantage of a map is its performance in locating an entry, given the key.

Maps We create an instance of a map: Map table;... table = new TreeMap(); We add the key-value pairs to the map: table.put(“CS0101”, “Great course. Take it.”); –The first argument is the key. –The second argument is the value.

Map Methods To remove an entry from a map, we use its remove method and pass the key as the argument. To retrieve all entries in the map, we use the entrySet method. To access all entries in a map, we use the entrySet method to obtain a set of entries, then use the iterator method as in the ArrayList example. The getKey and getValue methods are two particularly useful methods in the interface. These method retrieve the map entry’s key and value, respectively.