©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

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.
Java Review Interface, Casting, Generics, Iterator.
Chapter 10 Introduction to Arrays
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Sections 1-4.
©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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 Arrays.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Recitation 3/27/2009 CS 180 Department of Computer Science, Purdue University.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
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.
CS180 Recitation 25th/26th October, 2007 Department of Computer Science, Purdue University.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
©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. Chapter Chapter 10 Arrays and Collections.
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 N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Multiple Choice Solutions True/False a c b e d   T F.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
111 © 2002, Cisco Systems, Inc. All rights reserved.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
 Pearson Education, Inc. All rights reserved Arrays.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java SE 8 for Programmers, Third Edition
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
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.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
© 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.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Chapter 8: Part 3 Collections and Two-dimensional arrays.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Collections Dwight Deugo Nesa Matic
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays (adapted from the publisher’s.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Dynamic Data Structures and Generics
Object Oriented Programming in java
Arrays.
Presentation transcript:

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API *ArrayList *Map

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Information Represented as a Table Step -> Grade

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Two-Dimensional Arrays *In Java, tabular data may be stored in a two-dimensional array. *In a two-dimensional array, two indices (one for the row and one for the column in the table) are used to refer to the array element.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Two-Dimensional Arrays *To declare our example array, we write: double[][] payScaleTable; double payScaleTable[][]; *We instantiate the array as payScaleTable = new double[4][5];

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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]

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Processing a 2D Array *Nested-for loops are useful for manipulating two-dimensional arrays. *You need one loop for each index for (int i=0; i<4; i++) { for (int j=0; j<5; j++) System.out.format( "%6.2f", payScaleTable[i][j]); System.out.println(); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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. *This concept can be extended to higher dimensional arrays if necessary.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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]; payScaleTable[3] = new double [5];

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Instanitating a 2D Array

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Instantiation, continued

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Size of Two-Dimensional Arrays *The expression payScaleTable.length refers to the length of the payScaleTable array itself.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Size of Two-Dimensional Arrays *The expression payScaleTable[1].length refers to the length of the array stored at row 1 of payScaleTable.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Subarrays *An array that is part of another array is called a subarray. Also sometimes called a slice. *In Java, we get a subarray by giving just the first index of a 2D array double [] grade1 = payScaleTable[0];

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Initializing 2D arrays *An array of arrays may be initialized when it is created. *Similar to a 1D array except need to group elements into subarrays double [][] payScaleTable = {{10.50, 12.00, 14.50, 16.75, 18.00}, {20.50, 22.25, 24.00, 26.25, 28.00}, {34.00, 36.50, 38.00, 40.35, 43.00}, {50.00, 60.00, 70.00, 80.00, 99.99}};

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Changing the size of an array *The size of an array is determined when it is instantiated. *What do we do if we discover we need more elements than were allocated? 1.Use new to allocate a larger array with a temporary reference 2.Copy the elements from the original array 3.Set the original reference to the new array

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lists and Maps *The java.util library contains classes for maintaining collections of objects. *These classes contain methods that make it easy to maintain collections whose size needs to change. *These classes are collectively referred to as the Java Collection Framework (JCF).

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lists *The List interface is one 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. Create an instance of a class that implements List *List and the classes that implement it are examples of Generic classes - they can be implemented with elements of any object type.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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. See Chapter 15

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Creating an ArrayList *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 ( ); You can use any type where Object appears above Using Object means the list can contain any object *Using List for the type of myList permits a quick change of the implementation class if necessary.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lists *The default constructor creates an empty list with an initial capacity of 10. *As you add elements, the capacity will grow as needed. This is useful when you don't know how many elements you will have. *Increasing the capacity of a list is not very efficient so if the necessary capacity is known, it is better to create a list with the actual capacity needed.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. List Methods *The add method allows us to add objects to the list. myList.add( new Object()); *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. int count = myList.size();

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. List Methods *The remove method takes an element’s index as its parameter and allows us to remove an element from a list. Object o = myList.remove( 2); *The get method allows us to access objects stored in a list by giving their index position in the list. Object o = myList.get( 2);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. List Methods *The iterator method returns an object that implements the Iterator interface. Iterator iter = myList.iterator(); *We can use the iterator to scan through the elements of the list without using an index. while (iter.hasNext()) System.out.println( iter.next());

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. List Iterators *Iterator is an interface that supports two methods. 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lists *The iterator method is supported by most of the collection classes. *A list cannot include primitive data values as its elements, but wrapper classes can adapt the values to acceptable list objects.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Other Collection Interfaces *Maps A map consists of entries. Each entry is divided into two parts: key Value You use the key to look up a value (like looking up a word in a dictionary) *Tree A tree organizes a collection in a non-linear way Think of an organization chart