Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.

Slides:



Advertisements
Similar presentations
Programming Logic and Design Sixth Edition
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
C++ for Engineers and Scientists Third Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
 Pearson Education, Inc. All rights reserved Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
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: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Array Processing.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 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.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
8-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Arrays Chapter 8.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 8 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter 6: Using Arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Starting Out with Programming Logic & Design
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Programming Logic and Design Fifth Edition, Comprehensive
Programming In Visual Basic .NET
Arrays.
Presentation transcript:

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 2 McGraw-Hill/Irwin Objectives Establish an array of variables and refer to individual elements in the array with variable subscripts. Use a for loop to step through an array. Use a loop to look up a matching value in an array. Accumulate totals using array elements. Distinguish between direct access and indirect access of a table. Combine the advantages of list objects with arrays. Store data in multidimensional arrays. Create and use an array of objects.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 3 McGraw-Hill/Irwin Arrays An array is a series (or list) of values that all have the same data type and all have the same name. An array is used when you need to keep a series of values for later use such as reordering, calculating, or printing. An array can be a list of variables or a list of objects. Imagine trying to enter data for 500 products and each time the data is entered it has to be saved somewhere to enter the data for the next product. That somewhere is an array. To store multiple values use an array. An array is a series of individual variables, all referenced by the same name.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 4 McGraw-Hill/Irwin Arrays Arrays are sometimes called lists, tables, or subscripted variables. The position in the array is represented by a subscript. For example strName[0], strName[1], … the numbers are subscripts. The subscripts (also known as index) are always in square brackets. The individual variable is called an element in the array. The first element in the array always starts with a subscript of zero. If you have 10 elements, the subscripts will go from 0 to 9.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 5 McGraw-Hill/Irwin Subscripts Subscripts may be constants, variables, or numeric expressions. Although, subscripts have to integers, the non-integers are rounded off. To know how many elements in the array, you must specify the number of elements in the array at the time of initialization.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 6 McGraw-Hill/Irwin Declaring an Array You can choose several techniques for a declaring an array.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 7 McGraw-Hill/Irwin Using the new Keyword to declare an Array—General Format DataType VariableName[] = new DataType [size]; DataType[] VariableName = new DataType[size];

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 8 McGraw-Hill/Irwin Using the new Keyword to Declare an Array—Examples int intEmployeeCount[] = new int[50]; float[] fltTotal = new float[10]; String strName[] = new String[25];

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 9 McGraw-Hill/Irwin Declaring an Array The declaration statement allocates storage for a specified number of elements and initializes each element to the default value for the data type. For example, for an integer array all the elements will be initialized to 0 and for a String array all the elements will be initialized to empty string.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 10 McGraw-Hill/Irwin

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 11 McGraw-Hill/Irwin Using an Initializer to Create an Array—Examples int intDepartmentNumber[] = {423, 633, 672, 981}; String strDepartmentNames[] = {"Accounting", "Information Systems", "Marketing", "Sales"};

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 12 McGraw-Hill/Irwin More on Subscripts A subscript must reference a valid element in the array. If an array has 10 elements in the list and you try and reference the 15th element in the list, array will throw an exception. Java rounds up fractional subscripts such as 2 1 / 2.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 13 McGraw-Hill/Irwin Iterating Through an Array You can use a for loop to iterate through all the elements in an array. The following loop initializes a loop index to 0 to access the first element in the fltSales array. As intIndex is incremented each element is added to the total. for(int intIndex = 0; intIndex < 10; intIndex ++) { fltTotal += fltSales[intIndex]; }

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 14 McGraw-Hill/Irwin Using Array Elements for Accumulators Array elements are regular variables and perform in the same ways as all variables used so far. You may use subscripted variables for counters or accumulators.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 15 McGraw-Hill/Irwin Catching User Input Errors If the user enters an invalid number you can check he range with an if statement or catch the exception. If you choose to catch an exception rather than test with an if statement, check for an ArrayIndexOutOfBoundsException. Using the group number, as an index to the array is a technique called direct reference.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 16 McGraw-Hill/Irwin Shows the Applet and the Variables Used intTotal[] array [0] [1] [2] [3] [4] [5] [6] [7] intGroupNum (txtGroup –1) 3 txtGroup txtSale

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 17 McGraw-Hill/Irwin Table Lookup Considering the last example of the troops, whose numbers were from 1 to 8. It is not always that the groups have numbers that are sequentially numbered. The best way is to declare two arrays. One array that holds the group numbers and the other array holds the total sales. The user will type the group number and the sale. The array will look up the group number and the subscript of the group number will be used in the array for the total sales. The technique used to find the subscript is called table lookup. The subscript will serve to keep track of the total for each group.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 18 McGraw-Hill/Irwin Each Element in the intGroupNumArray Corresponds to One Element of the intTotal Array [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [4] [5] [6] [7] intGroupNumArray[ ] intTotal[ ] array

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 19 McGraw-Hill/Irwin [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [4] [5] [6] [7] intGroupNumArray[ ]intTotal[ ] array txtGroup txtSale intGroupNum 110 intGroupSub 2 A Lookup Operation

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 20 McGraw-Hill/Irwin Flowchart of the Logic of a Table Lookup Operation

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 21 McGraw-Hill/Irwin Using Lists with Arrays Instead of storing information in an array for the group numbers, store them in a List or Choice component. This is would be a more friendly and efficient solution. The user can select the group number from the list and the index can be used for the array. The getSelectedIndex method will determine the array subscript. You can print the group numbers, using the getItem method.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 22 McGraw-Hill/Irwin Allow the User to Select a Group Using the Choice List

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 23 McGraw-Hill/Irwin Multidimensional Arrays You may need two or more subscripts to identify tabular data, where data is arranged in rows and columns.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 24 McGraw-Hill/Irwin The Declaration Statement for Two- Dimensional Arrays—General Format DataType ArrayName[NumberRows][NumberColumns];

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 25 McGraw-Hill/Irwin The Declaration Statement for Two- Dimensional Arrays—Examples String strName[][] = new String[3][4]; float fltRate[][] = {{1.0f, 1.5f, 1.65f, 1.85f}, {1.58f, 2.0f, 2.4f, 3.05f}, {1.71f, 2.52f, 3.10f, 4.0f}, {2.04f, 3.12f, 4.0f, 5.01f}, {2.52f, 3.75f, 5.10f, 7.25f}};

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 26 McGraw-Hill/Irwin Multidimensional Arrays The strName example has three rows and four columns. The second example uses an initializer to set values, one row at a time. You must always use two subscripts when referring to individual elements of the table. Specify the row with first subscript and the column with second subscript. The elements of the array may be used in the same ways as any other variable – in accumulators, counts, reference field, displaying text and conditions.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 27 McGraw-Hill/Irwin Summing a Two-Dimensional Table You can find the sum of a table in various ways. You may sum either the columns or the rows of the table; or as in a cross-foot, or you may sum the figures in both directions and double-check the totals. To sum the array in both directions each column needs one total field, and each row needs one total field.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 28 McGraw-Hill/Irwin Two One-Dimensional Arrays Hold Totals for the Two-Dimensional Array

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 29 McGraw-Hill/Irwin Lookup Operation for Two- Dimensional Tables When you look up items in a two-dimensional table, you can use the same techniques discussed with single dimensional arrays: direct reference and table lookup. The limitations are the same. Direct reference – row and column subscripts must be readily available. A table lookup – most common technique; require additional one-dimensional arrays or list to aid in the lookup process.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 30 McGraw-Hill/Irwin This Shipping Rate Table in a Two-Dimensional Array Can Be Used to Look Up the Correct Shipping Charges Shipping Rate Table fltRate array 1 lb. >10 lb. 10 lb. 5 lb. 3 lb. Zone A Zone B Zone C Zone D Weight (not to Exceed) intWeightSub 1 intZoneSub 3 intWeightSub uses the index of 1stWeight intZoneSub uses the index of 1stZone

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 31 McGraw-Hill/Irwin Using TextFields If you are using text fields for data entry rather than lists, the input requires more validation. You must validate both the entries before you can find the correct value.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 32 McGraw-Hill/Irwin Creating an Array of Objects You can create arrays of classes or create arrays of components. Using a class requires the keyword new to create an array of objects. For example: Course Courses[] = new Course[3]; Label lblGroupNumber[] = new Label[8]; The objects in the array are not automatically instantiated. Use a loop to instantiate each object, once again using the keyword new. Usually you will place the following code in the init.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 33 McGraw-Hill/Irwin FindCourseApplet Looks Up the Course Number in the Courses Array and Fills in the Room and Unit Information Courses[0].strRoom Courses[0].fltUnits Courses array CIS 10A26D COMP A3.5 CIS Courses[0] Courses[1] Courses[2] strDescstrRoom fltUnits

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 34 McGraw-Hill/Irwin An Array of Course Objects Instead, you can refer to objectName-dot-variableName (Courses[0].strRoom).

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 35 McGraw-Hill/Irwin An Array of Components You can also create arrays of components. This technique is handy when you need several components of the same type, especially if you need to reference the components with an index.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 36 McGraw-Hill/Irwin Use Two Arrays of Labels to Display the Group Numbers and the Total for Each Group

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 37 McGraw-Hill/Irwin Java Arrays for C++ and Visual Basic Programmers Arrays in Java must have defined limits differing from those in C++. Java does not allow pointer arithmetic to access array elements. Both features enhance security in Java. In Visual Basic, you can declare array with subscripts beginning with any number, such as (1 to 10) or (-10 to 10), which Java does not allow. Visual Basic also has dynamic arrays, which can be resized during program execution.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 38 McGraw-Hill/Irwin Vectors In reality, Java does have resizable or dynamic arrays – they are called vectors. A Vector is a class that is similar to an array. It holds multiple values and is referenced by an index. In addition, a vector automatically grows when more elements are needed. The Vector class has many methods to aid creating the elements, adding and removing elements, and searching for particular elements.