Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Introduction to C Programming
Chapter 10 Introduction to Arrays
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Chapter 10.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
An Introduction to Programming with C++ Fifth Edition
©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.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Chapter 9: Arrays and Strings
 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
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five.
 Pearson Education, Inc. All rights reserved Arrays.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Java Unit 9: Arrays Declaring and Processing Arrays.
 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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 Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 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.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Computer Programming BCT 1113
Chapter 5: Arrays: Lists and Tables
Introduction To Programming Information Technology , 1’st Semester
Chapter 7 Part 2 Edited by JJ Shepherd
MSIS 655 Advanced Business Applications Programming
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index

In previous examples in the text data items (variables) were individual and isolated – except array lists. Often data is grouped into a collection of information Tables, telephone books, train time tables, bank statements Called data structures

All information in a table related in some way An array – table with a single row of information – –( an array of numbers) –John Paul George Ringo –( an array of Strings) In every array each item is known as an element, refered to by its position – called the index.

Each component, sometimes known as an element, subscript or index. Arrays in Java start with the zeroth element Indices not held in the computer – indeces are the way data in the computer are located the data the index

Array Operations Creation - needs type, length Put values in - initialize Display - on screen Search - locate values (data elements) Calculate – add, subtract, divide etc.

Creating an Array int [ ] ages = new int [6]; String [ ] band = new String [4]; –Again indices start at zero, are integer, go up to one less than the length of the array To input an integer element into the array ages – ages[2] = Integer.parseInt(textField.getText( )); – band[3] = textField.getText(); // gets element 3 of the band array

Output from an array textField.setText (“ The first age is “ + Integer.toString (ages [0] ) ); textField.setText (“ The 4 th band member is “ + band [3]; // remember start from zero! Changing values in an array: – ages [3] = 99; // no longer 3 – band [2] = “Mike”; // no longer George

Using variables instead of numbers (numeric literals) in square [ ] brackets is what gives power to Java array handling Example: computers sold in a store in one week int [ ] sales = new int [7]; Monday will be the zeroth element sale[0] = 13; // 13 sold on Monday

Sale : // index numbers Data : // data in array Int sum; sum = sale[0] + sale[1] + sale[2]…sale[6]; Loop using variable for index great tool int sum = 0; for ( int dayNumber = 0; dayNumber <= 6; dayNumber++) { sum = sum + sale[ dayNumber ]; }

Each time the loop is repeated the next value is added to the total of the sales array. The loop explicitly shows it is performing a systematic operation on an array

The index ‘dayNumber’ is clear and strongly related to the problem The length of an array – int size; – size will be equal to the table.length – size = table.length; //.length is a special feature in Java. It holds the size of the array Arrays are of a fixed size (length) once declared. Values of data within an aray may be changed and reused.

If new is used to redefine an array name all access to the old data is lost given: int [ ] arry = { 1,2,3}; int arry = new arry[ ]; // access to the data 1, 2 and 3 is lost.

Passing arrays as parameters The name of the array holds the address of the zeroth element private int sum ( int [ ] array) { For ( int index = 0; index < = array.length; index++) { – total = total + array [ index]; } – return total; } –The header of the method declares an array named array in this case, of int type, the array can be of any length. It is general purpose and potentially very useful

Using constants In programs that use several arrays, there are usually many loops, so various lengths might be needed: int [ ] studentMark = new int [ ]; int [ ] courseMark = new int [10]; Given 10 is both the length of both studentMark and courseMark…but, what if 20 students are needed? To clarify the problem use ‘final’

final int students = 20; final int courses = 20; Later … int studentMark = new int [students]; Int courseMark = new int [courses]; – for ( int index = 0; index < students; index++) { // body of loop … }

Initalizing an array int [ ] table new int [10] ; // an array (table) is set up and loaded with all zeros – all elements are set to 0. Or all blanks for String arrays and all nulls for arrays of objects. An explicitly loaded array: ages loaded – int [ ]ages = { 23, 54, 96, 13, 7, 32 }; –// indexes:

String [ ] band = { “John”, “Paul”, “George”, “Ringo” }; // loads names into the ‘band’ array int [ ] table = new int [25]; –For ( int index = 0; index < table.length; index++) { table[index] = 0;} // loads all zeros into array ‘table’

A sample program page Holds an array rain. A display method used to output the days of the week and the rainfall values for each day. a method Value (places a value into the array) A largest method (finds the day with the most rainfall in the array) private int [ ] rain = { 7, 8, 0, 4, 3, 8, 1 };

Rain methods Lookup – given an array height we can extract to a: double myHeight = height [25] ; –// the 25 th element above the [0]th element –Given: name (an array of names of days of the week) dayName = name[dayNumber]; –Would return the String element referenced by dayNumber.

Searching: given two String arrays names and numbers A telephone directory can be created for (index = 0;! ( names[index].equals (wanted) && ! (names[index].equals (“END”)); index++) { }; if (names[index].equals (wanted)) { nember.setText ( “Number is “ + numbers[index]) ; } else {numbers.setText (“ Name not found “);}

Perform a serial search Starts at beginning index 0 and continues to end of array. Each element is tested in turn. The telephone directory names could be in a file loaded into an array – searched as an array

Arrays of object Balloon program pages A method changes size, increases the diameter of an Oval (the ‘balloon’) Summary: Array a collection of data Given a name by programmer All array items are of the same type Declared: int [ ] array = new int [25]; Index starts at zero Largest index item 24 ( 0..24) for this example.