Chapter 7 Part 1 Edited by JJ Shepherd

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays Chapter 6 Chapter 6.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
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.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
COS 260 DAY 16 Tony Gauvin.
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Chapter 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
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.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
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 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chapter 7.
Arrays Chapter 7.
4. Java language basics: Function
An Introduction to Programming with C++ Sixth Edition
Computer Programming BCT 1113
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Chapter 8 Arrays Objectives
Arrays … The Sequel Applications and Extensions
Arrays, For loop While loop Do while loop
Visual Basic .NET BASICS
Chapter 8 Arrays Objectives
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Arrays .
Chapter 7 Part 2 Edited by JJ Shepherd
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Arrays ICS2O.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
Announcements Lab 6 was due today Lab 7 assigned this Friday
Building Java Programs
Chapter 9: Data Structures: Arrays
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 8 Arrays Objectives
Arrays.
Presentation transcript:

Chapter 7 Part 1 Edited by JJ Shepherd Arrays Chapter 7 Part 1 Edited by JJ Shepherd

Arrays!

Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Use an array not filled completely

Array Basics: Outline Creating and Accessing Arrays Array Details The Instance Variable length More About Array Indices Analyzing Arrays

Creating and Accessing Arrays An array is a special kind of object Think of as collection of variables of same type Creating an array with 7 variables of type double To access an element use The name of the array An index number enclosed in braces Array indices begin at zero

Creating and Accessing Arrays Figure 7.1 A common way to visualize an array Note sample program, listing 7.1 class ArrayOfTemperatures

A Thought I usually visualize an array as being a filing cabinet It is fix sized You store things of the same type (usually) in one

Array Details Syntax for declaring an array with new The number of elements in an array is its length ***The Length is fixed!*** The type of the array elements is the array's base type

Square Brackets with Arrays With a data type when declaring an array int [ ] pressure; To enclose an integer expression to declare the length of the array pressure = new int [100]; To name an indexed value of the array pressure[3] = keyboard.nextInt();

Array Details Figure 7.2 Array terminology

The Instance Variable length As an object an array has only one public instance variable Variable length Contains number of elements in the array It is final, value cannot be changed

More About Array Indices Index of first array element is 0 Last valid Index is arrayName.length – 1 Array indices must be within bounds to be valid When program tries to access outside bounds, run time error occurs OK to "waste" element 0 Program easier to manage and understand Yet, get used to using index 0

Initializing Arrays Possible to initialize at declaration time Also may use normal assignment statements One at a time In a loop

Example! Closest Without Going Over

Another Game! Closest Without Going Over

Array Assignment and Equality Arrays are objects Assignment and equality operators behave (misbehave) as specified in previous chapter Variable for the array object contains memory address of the object Assignment operator = copies this address Equality operator == tests whether two arrays are stored in same place in memory ***Just like Strings you cannot use the double equals to check equality*** You have to go through each value of the array to determine if they are equal

Array Assignment and Equality Note results of == Note definition and use of method equals Receives two array parameters Checks length and each individual pair of array elements Remember array types are reference types

Gotcha – Don’t Exceed Array Bounds The code below fails if the user enters a number like 4. Use input validation. Scanner kbd = new Scanner(System.in); int[] count = {0,0,0,0};   System.out.println("Enter ten numbers between 0 and 3."); for (int i = 0; i < 10; i++) { int num = kbd.nextInt(); count[num]++; } for (int i = 0; i < count.length; i++) System.out.println("You entered " + count[i] + " " + i + "'s");

Partially Filled Arrays Array size specified at definition Not all elements of the array might receive values This is termed a partially filled array Programmer must keep track of how much of array is used

Partially Filled Arrays Figure 7.4 A partially filled array

Sorting, Searching Arrays: Outline Selection Sort Other Sorting Algorithms Searching an Array

Selection Sort Consider arranging all elements of an array so they are ascending order Algorithm is to step through the array Place smallest element in index 0 Swap elements as needed to accomplish this Called an interchange sorting algorithm

Selection Sort Figure 7.5a

Selection Sort Figure 7.5b . . .

Selection Sort Algorithm for selection sort of an array

Example! Selection Sort

Other Sorting Algorithms Selection sort is simplest But it is very inefficient for large arrays Java Class Library provides for efficient sorting Has a class called Arrays Class has multiple versions of a sort method

Searching an Array Method used in OneWayNoRepeatsList is sequential search Looks in order from first to last Good for unsorted arrays Search ends when Item is found … or … End of list is reached If list is sorted, use more efficient searches