Chapter 8: Collections: Arrays

Slides:



Advertisements
Similar presentations
Computer Science 209 Software Development Equality and Comparisons.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
©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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
C++ for Engineers and Scientists Third Edition
A First Book of ANSI C Fourth Edition
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Java – Part II Lecture Notes 4. Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates)
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 The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
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.
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.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Objectives You should be able to describe: One-Dimensional Arrays
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays Chapter 7.
The need for Programming Languages
Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Arrays in JAVA Visit for more Learning Resources.
Lectures linked lists Chapter 6 of textbook
Chapter 2 Elementary Programming
Chapter 5 Ordered List.
Chapter 7: Working with Arrays
Computer Programming BCT 1113
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Chapter 7 Part 1 Edited by JJ Shepherd
SELECTION STATEMENTS (1)
An Introduction to Java – Part I
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Java How to Program, Late Objects Version, 10/e
Chapter 5 Ordered List.
Pass by Reference, const, readonly, struct
Chapter 8 Slides from GaddisText
CHAPTER 6 GENERAL-PURPOSE METHODS
Introduction To Programming Information Technology , 1’st Semester
Defining methods and more arrays
Lec 14 Oct 23, 02.
Can store many of the same kind of data together
Arrays and Collections
Object Oriented Programming in java
CS2011 Introduction to Programming I Arrays (II)
Data Structures (CS212D) Week # 2: Arrays.
Chapter 6 Methods.
Objectives You should be able to describe: Addresses and Pointers
Week 4 Lecture-2 Chapter 6 (Methods).
Java Programming: Chapter 9: Recursion Second Edition
Java Basics Data Types in Java.
Presentation transcript:

Chapter 8: Collections: Arrays Searching and Sorting Arrays as Arguments

Aggregate Data Types – re-visited Some objects are made up of other objects – e.g. a computer system. Aggregation is sometimes described as a “has-a” relationship. For instance, a car has 4 wheels. In software, we define an aggregate data type consisting of one or more scalar data type objects

Run-Time Dimensioning The size of an array can also be entered interactively at run time An entered value can be used to allocate space for an array using the new operator

public static void main(String[] args) { int i, numgrades; int grade[]; // declare the array String s1; s1 = JOptionPane.showInputDialog("Enter the number of grades” + “ to be processed: "); numgrades = Integer.parseInt(s1); grade = new int[numgrades]; // allocate the array

The Arrays Class: Searching and Sorting Java-provided class Distinct from the Array class we have been implementing Called helper class because it provides a number of extremely useful methods that can be a great help in processing arrays Contains methods for: Sorting an array Searching a sorted array for a particular item

The sort() and binarySearch() Methods sort() method Arranges elements of array into ascending (increasing) order Uses modified quicksort algorithm – we’ll look at this in more detail in 8.9 Only works for: Primitive data types Strings

The sort() and binarySearch() Methods (continued) Searches array for a specified value Requires a sorted array Returns: Item index if found Negative number if not found

Search & Sort Generally the sort( ) method is called immediately before the binarySearch( ) method. Use either import java.util.*; or import java.util.Arrays; Example:

String s1; int i, numels; int variousNums[]; int item, location; // set up the basic input stream BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter the number of array values: "); s1 = br.readLine(); numels = Integer.parseInt(s1); // allocate the array variousNums = new int[numels]; // read the array values for (i = 0; i < numels; i++) { System.out.print("Enter element " + (i+1) + ": "); s1 = br.readLine();_ variousNums[i] = Integer.parseInt(s1); }

// sort the array Arrays // sort the array Arrays.sort(variousNums); // display the sorted array System.out.print("The values in sorted order are:"); for (i = 0; i < numels; i++) System.out.print(" " + variousNums[i]); // now locate a desired element System.out.print("\n\nEnter the item you are searching for: "); s1 = br.readLine(); item = Integer.parseInt(s1); location = Arrays.binarySearch(variousNums, item); if (location > 0) System.out.println("This item is at location " + (location + 1) + " in the sorted array."); else System.out.println("This item is not in the list."); } }

Arrays as Arguments Individual array elements are passed to a called method in the same manner as individual scalar variables Passed by value – i.e. method receives a copy of the stored value, can manipulate it, but cannot directly change the value in the original array. e.g. findMax (number[5], number[7])

Passing an entire array A complete array can be passed by reference The called method may change items in the original array double salary [ ] = new double [5]; …. findMax(salary); Must alert the method that an array is coming in header line: double findMax (double vals [ ])

public class ArrayArgument { public static void main(String[] args) { int nums[ ] = {2, 18, 1, 27, 16}; // the call is made here System.out.println("The maximum value is " + findMaximum(nums)); } // this is the called method used to find the maximum value // stored in an array of integers public static int findMaximum(int vals[]) { int i, max = vals[0]; for (i = 1; i < vals.length; i++) if (max < vals[i]) max = vals[i]; return max; } }

figure 8.14 on page 418 illustrates that the method references the original array.

Class Exercise p. 416 # 1, 2 p. 420 # 1, 3, 5, 7