Chapter 11 Arrays. Introduction Array: An ordered collection of values with two distinguishing characters: – Ordered and fixed length – Homogeneous. Every.

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.
The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Arrays and ArrayLists C H A P T E R slides partially adapted from.
Chapter 10 Introduction to Arrays
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Simple Arrays Eric Roberts CS 106A February 10, 2010.
©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.
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.
Alice in Action with Java
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
Chapter 9: Advanced Array Concepts
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Data Strcutures.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java SE 8 for Programmers, Third Edition
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Data structures Abstract data types Java classes for Data structures and ADTs.
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.
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.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Simple Arrays Eric Roberts CS 106A February 12, 2016.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Chapter 6 Arrays Solution Opening Problem
Simple Arrays Eric Roberts CS 106A February 15, 2017.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Can store many of the same kind of data together
Object Oriented Programming in java
ArrayLists 22-Feb-19.
CS106A, Stanford University
Java Programming Language
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Arrays.
Presentation transcript:

Chapter 11 Arrays

Introduction Array: An ordered collection of values with two distinguishing characters: – Ordered and fixed length – Homogeneous. Every value in the array must be of the same type The individual values in an array are called elements. The number of elements is called the length of the array Each element is identified by its position number in the array, which is called index. In Java, the index numbers begin with 0.

Array declaration An array is characterized by Element type Length type[ ] identifier = new type[length]; Default values in initialization numerics 0 boolean false objects null

An array of objects Elements of an array can be objects of any Java class. Example: An array of 5 instances of the student class Student[] topStudents = new Student[5];

Defining length Use named constant to declare the length of an array. private static final in N_JUDGES = 5; double[ ] scores = new double[N_JUDGES]; Or read the length of an array from the user.

Selecting elements Identifying an element array[index] Index can be an expression Cycling through array elements for (int i = 0; i < array.length; i++) { operations involving the ith element }

Human-readable index values From time to time, the fact that Java starts index numbering at 0 can be confusing. Sometimes, it makes sense to let the user work with index numbers that begin with 1. Two standard ways: 1.Use Java’s index number internally and then add one whenever those numbers are presented to the user. 2.Use index values beginning at 1 and ignore the first (0) element in each array. This strategy requires allocating an additional element for each array but has the advantage that the internal and external index numbers correspond.

Internal representation of arrays Student[] topStudents = new Student[2]; topStudents[0] = new Student(“Abcd”, ); FFB8 FFBC FFC topStudents stack C 1010 length topStudents[0] topStudents[1] 2 null heap

Student[] topStudents = new Student[2]; topStudents[0] = new Student(“Abcd”, ); C C C C Ab cd false null length topStudents[0] topStudents[1] length studentName studentID creditsEarned paidUp 1000topStudentsFFB8 FFBC FFC0

Passing arrays as parameters Recall: Passing objects (references) versus primitive type (values) as parameters. Java defines all arrays as objects, implying that the elements of an array are shared between the callee and the caller. swapElements(array[i], array[n – i – 1]) (wrong) swapElements(array, i, n – i – 1)

private void swapElements(int[] array, int p1, int p2) { int tmp = array[p1]; array[p1] = array[p2]; array[p2] = tmp; } Every array in Java has a length field. private void reverseArray(int[] array) { for (int i = 0; i < array.length / 2; i++) { swapElements(array, i, array.length – i – 1); }

Using arrays Example: Letter frequency table Design a data structure for the problem Array: letterCounts[ ] index : distance from ‘A’ index = Character.toUpperCase(ch) – ‘A’ letterCounts[0] is the count for ‘A’ or ‘a’

A convenient way of initializing an array: int[ ] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; private static final String[ ] US_CITIES_OVER_ONE_MILLION = { “New York”, “Los Angeles”, “Chicago”, “Huston”, “Philadelphia”, “Phoenix”, “San Diego”, “San Antonio”, “Dallas”, }

Arrays and graphics Arrays turn up frequently in graphical programming. Any time that you have repeated collections of similar objects, an array provides a convenient structure for storing them. As an aesthetically pleasing illustration of both the use of arrays and the possibility of creating dynamic pictures using nothing but straight lines the text presents YarnPattern program, which simulates the following process: – Place a set of pegs at regular intervals around a rectangular border – Tie a piece of colored yarn around the peg in the upper left corner – Loop that yarn around that peg a certain distance DELTA ahead – Continuing moving forward DELTA pegs until you close the loop

Two-dimensional arrays Each element of an array is an array (of the same dimension) int[][] A = new int[3][2] An array of three arrays of dimension two A[0][0]A[0][1] A[1][0]A[1][1]A[2][0] 3-by-2 matrix

Memory allocation (row orientation) A[0][0 ] A[0][1] A[1][0] A[1][1] A[2][0] A[2][1]

Initializing a two-dimensional array Static int A[3][2] = { {1, 4}, {2, 5}, {3, 6} }; A 3-by-2 matrix

The ArrayList Class Although arrays are conceptually important as a data structure, they are not used as much in Java as they are in most other languages. The reason is that the java.util package includes a class called ArrayList that provides the standard array behavior along with other useful operations. ArrayList is a Java class rather than a special form in the language. As a result, all operations on ArrayList s are indicated using method calls. For example, – You create a new ArrayList by calling the ArrayList constructor. – You get the number of elements by calling the size method rather than by selecting a length field. – You use the get and set methods to select individual elements.

Methods in the ArrayList class Figure 11-12, p. 443, where is the base type. boolean add( element) remove(int index) int indexOf( value) An ArrayList allows you to add new elements to the end of a list. By contrast, you can’t change the size of an existing array without allocating a new array and then copying all the elements from the old array into the new one.

Linking objects Objects in Java can contain references to other objects. Such objects are said to be linked. Linked structures are used quite often in programming. An integer list: public class IntegerList { public IntegerList(int n, IntegerList link) { value = n; next = link; } /* Private instance variables */ private int value; private IntegerList next; }

Linked structures Java defines a special value called null to represent a reference to a nonexistent value and can be assigned to any variable that holds an object reference. Thus you can assign null to the next field of the last element to signify the end of the list. You can insert or remove an element from a list. The size of a linked structure can change. Also, elements of a linked structure can be objects. A simple example: SignalTower class, Figure 7-3, p. 242.

Arrays vs. linked lists The two attributes that define a data type are: domain and a set of operations. An array is a collection of items of the same type. It is efficient to select an element. The addresses of array[i] is the address of array + sizeof(overhead) + i*sizeof(type). For example, if the type is int, then sizeof(int) is 4. Since the array size is fixed, it is hard to insert or delete an element. The items on a list can have different types. Linked lists can represent general structures such as tree. Items can be inserted to or removed from a list. However, to select an element, you have to follow the links starting from the first item on the list (sequential access).