Alice in Action with Java

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
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.
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Written by: Dr. JJ Shepherd
Chapter 10 Introduction to Arrays
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
Chapter 7 – Arrays.
Programming with Collections Collections in Java Using Arrays Week 9.
Hash Tables1 Part E Hash Tables  
© 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.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
Java Unit 9: Arrays Declaring and Processing Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using C++ 2E
Chapter 9: Advanced Array Concepts
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 8 Arrays and Strings
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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 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.
Chapter 8: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
List Interface and Linked List Mrs. Furman March 25, 2010.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
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: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
int [] scores = new int [10];
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
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.
Chapter VII: Arrays.
Computer Programming BCT 1113
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Object Oriented Programming in java
Presentation transcript:

Alice in Action with Java Chapter 12 Arrays and Lists in Java

Objectives Understand Java’s array data structure Solve problems using Java’s LinkedList data structure Solve problems using Java’s ArrayList data structure Alice in Action with Java

Arrays and Lists in Java Features common to arrays and lists Used to define variables that store groups of items Provide access to a given item using an index Differences between arrays and lists Array’s size is fixed at runtime, a list’s size can change Array stores items using less memory than a list Array provides direct item access faster than a list Alice in Action with Java

Introductory Example: Air Pollution Reporting Review of AirPollutionIndex.java Read air pollution level readings from five points Compute and display the average reading (the index) Additional requirements of the enhanced program Display the five readings used to compute the average High-level algorithm for AirPollutionReport Build an array named readings with length = 5 Read the air pollution readings into readings Compute and display average of values in readings Display the individual values in readings Alice in Action with Java

Java Arrays General pattern for defining (declaring) an array Item[] anArray = new Item[N]; Item: specifies the items type Brackets tell compiler that anArray is an array handle new operator: allocates memory for the array N: an integer that specifies the array length (size) An example of an array definition: double[] readings = new double [NUM_READINGS]; NUM_READINGS is an integer constant = 5 readings is a handle to a 5 unit double type array Alice in Action with Java

Java Arrays (continued) Element: indexed unit variable in an array Item: value stored in an array element Items are initialized to default values for array’s type Example: default value for item in double type is 0.0 Creating an array parameter Place brackets between parameter’s type and its name General form: public ReturnType methodName( Item[] parameterName ){… Example: public static double average(double [] anArray) Alice in Action with Java

Java Arrays (continued) Alice in Action with Java

Java Arrays (continued) Components needed to access an array’s elements Array’s handle, item’s index, subscript operator ([]) Pattern for accessing an element: anArray[i] anArray is the handle to the array i: index value, which must be a non-negative integer An index value out of bounds throws an exception Example of an array access: readings[0] Accesses the first element of the readings array Note: index is off by one relative to item’s cardinal order length property: returns number of items in an array Alice in Action with Java

Java Arrays (continued) for loop provides convenient access to an array Example of an array traversal using a for loop for (int i = 0; i < arr.length; i++){ System.out.println("Reading #" + (i+1) + ": " + arr[i]);} for each loop Special loop used to read each item in an array Limitation: cannot be used to write to array’s elements Example of array traversal using a for each loop for (double item : anArray){sum += item;} Alice in Action with Java

Example 2: Month Names From Numbers Essential elements of user story Randomly generate a month number Query user for a month name matching the number Read the user’s response Display a message appropriate to the response Let the user keep playing as long as he or she wants Instance variables declared in the Month class An integer myNumber and a string myName Role of the Month()constructor Construct a Month object given a month number Alice in Action with Java

Example 2: Month Names From Numbers (continued) MONTHS will be used to store the month names The array is declared as a constant class variable Declaration also includes an initialization values list The array functions as a lookup table for the constructor Other Month members Accessors for the instance variables A toString()method Other classes used in MonthTester Random (to generate a random number) Scanner (to read in the month’s name) Alice in Action with Java

Example 2: Month Names From Numbers (continued) Alice in Action with Java

Arrays and Memory An array is a random access data structure Contiguous elements are accessed in constant time Subscript operation computes an element’s address How to compute the address of an array element Multiply the index i by the size of an item Add the resulting product to the starting address Example: anArray[4]= (anArray+4*itemSize) Insertion and removal are linear time operations Up to length–1 items are shifted in each operation Many of these operations can slow down a program Alice in Action with Java

Arrays and Memory (continued) Alice in Action with Java

Arrays and Memory (continued) Alice in Action with Java

Multidimensional Arrays Dimension: axis used to specify element’s location length determines space for one-dimensional array You can create arrays with multiple dimensions Example: two-dimensional array to model a table How to declare an N-dimensional array handle Use N pairs of brackets to declare Example of declaring a two-dimensional array private double [][] myTable = null; Alice in Action with Java

Multidimensional Arrays (continued) Defining a multidimensional array with default values Use the new operator and specify dimensions Ex: myTable = new double[rows][columns]; Defining multidimensional array with non-default values Use an initialization list Accessing a multidimensional array element Use N subscript operators for N dimensions Example: myTable[row][col] = item; Processing multidimensional arrays Use N for loops to traverse array with N dimensions Alice in Action with Java

Lists in Java Interface: a structure that only declares methods A class implementing an interface defines methods Example: String implements CharSequence Java’s List is an interface Two classes implementing List LinkedList: similar to Alice’s list data structure ArrayList: dynamic container offering fast access Alice in Action with Java

Lists in Java (continued) Alice in Action with Java

Lists in Java (continued) Alice in Action with Java

Using ArrayLists ArrayList implements the List interface Features of an ArrayList Reference type data structure Can increase in size during execution set()and get()access items in constant time Patterns for declaring and defining an ArrayList Insert items in an ArrayList using add() Alice in Action with Java

Using ArrayLists Rules for declaring an array list The type of an array list must be a reference type Use a wrapper class when a primitive type is needed Wrapper class contains primitive type plus operations Pattern for declaring an ArrayList ArrayList<ItemType> handleName = null; Example of a ArrayList declaration ArrayList<Double> listOfNumbers = null; Produces a handle called listOfNumbers listOfNumbers is capable of storing an arbitrary number of Double objects Alice in Action with Java

Using ArrayLists (continued) Alice in Action with Java

Using ArrayLists (continued) Alice in Action with Java

Summary Array: fixed-size container used to store items of the same type Items: values stored in an array’s elements Accessing an array element is a constant time operation Inserting and removing items in an array are linear time operations Arrays can have an arbitrary number of dimensions ArrayList includes the random access capability of arrays and the ability to change size Alice in Action with Java