11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

8/7: Ch. 7: Arrays What is an array? Declaring & allocating arrays Program of the day.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Computer Science I Arrays Professor: Evan Korth New York University.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference Parameters.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
 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.
1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
8/9: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
4/15: Searching Arrays Look at BubbleSort.java Searching arrays: the linear search Searching arrays: the binary search.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
 Pearson Education, Inc. All rights reserved Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 Pearson Education, Inc. All rights reserved Arrays.
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.
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.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
11/30: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays:
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
8/2: Recursion About Scoping.java Recursion Program of the Day.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
 2005 Pearson Education, Inc. All rights reserved Arrays.
8/8: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Arrays A few types Structures of related data items
Presentation transcript:

11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Arrays: what are they? a series of elements; a list. grocery list could be an array: –milk –bread –eggs –frozen pizza –juice –apples –oranges –Ramen noodles

Arrays: what are the positions? every item has a place; a position number. placeitem 1. milk 2. bread 3. eggs 4. frozen pizza 5. juice 6. apples 7. oranges 8. Ramen noodles

Arrays: how are they numbered? Java begins numbering at 0. placeitem 0. milk 1. bread 2. eggs 3. frozen pizza 4. juice 5. apples 6. oranges 7. Ramen noodles

Arrays: how are they named? Java names the list and refers to the place of the items in square brackets after the list name. groceryList [0] =milk groceryList [1]=bread groceryList [2] =eggs groceryList [3] =frozen pizza groceryList [4] =juice groceryList [5] =apples groceryList [6] =oranges groceryList [7] =Ramen noodles

Arrays: what are the elements? The individual items are called elements. The reference numbers are position numbers,or subscripts. groceryList [0] =milk groceryList [1]=bread groceryList [2] =eggs groceryList [3] =frozen pizza groceryList [4] =juice groceryList [5] =apples groceryList [6] =oranges groceryList [7] =Ramen noodles elements subscripts

Arrays: about the position numbers Subscripts must be integers or integer expressions. groceryList [0] =milk groceryList [1]=bread groceryList [2] =eggs groceryList [3] =frozen pizza groceryList [4] =juice groceryList [5] =apples groceryList [6] =oranges groceryList [7] =Ramen noodles

Arrays: function examples in Java an array of integers: list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9 list.length = 7 list [0] + list [2] = 2 list [1 + 3] = 8

Declaring and allocating an array to declare and allocate an array: int list[]; list = new int[7]; or int list[] = new int[7]; Another example: String args[] = new String[5]; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9

Declaring & initializing an array to declare and initialize an array: int list[] = {5,10,-3,-7,8,1,9}; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9

initializing elements of an array to initialize an array element: list[6] = 9; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9

StudentPoll.java: pt. 1 //fig. 7.7: StudentPoll.java import javax.swing.*; public class StudentPoll { public static void main ( String args[] ) { int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8,10, 1, 6, 3, 8, 6,10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8,10}; int frequency[] = new int [ 11 ]; String output = "";

About StudentPoll.java: pt. 2 for ( int a = 0 ; a < responses.length; a++ ) ++frequency [ responses [ a ] ]; output += "Rating\tFrequency\n"; for ( int r = 1; r < frequency.length ; r++ ) output += r + "\t" + frequency[r] + "\n";

About StudentPoll.java: pt. 3 JTextArea outputArea = new JTextArea (11,10); outputArea.setText( output ); JOptionPane.showMessageDialog ( null, outputArea, "Student Poll Program", JOptionPane.INFORMATION_MESSAGE ); System.exit ( 0 ); }

About “.length” length is actually an instance variable, indicating the number of elements in the array. We don’t have to declare it separately – it’s an automatic part of an array that we declare.

Passing arrays to methods Two ways: –passing the entire array as a whole –passing individual elements of the array The method that modifies the array must have an appropriate input type (parameter). Accepting an array: public void x ( int a[] ) Accepting elements: public void y ( int c ) public void z ( String d )

Getting more experience with arrays Pg 273: InitArray.java – declaring & allocating Pg 274: InitArray.java – initializing to values Pg 275: InitArray.java – using a for loop to set values Pg 276: SumArray.java – using a for loop to add up elements of an array

1 st Programs of the day pg. 278: StudentPoll.java After getting the program to run, modify the program to allow the user to input the array elements. –use a JOptionPane.showInputDialog to get the data from the user, and assign the CONVERTED value you get from there into the next array position. Use a for loop to provide the repetition and movement in the array position. Pg. 284 PassArray.java

Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the binary search

PassArray.java -- pt. 1 //Fig 7.10: PassArray.java //Passing arrays and individual elements to methods import java.awt.Container; import javax.swing.*; public class PassArray extends JApplet { JTextArea outputArea; String output; public void init() { outputArea = new JTextArea(); Container c = getContentPane(); c.add( outputArea );

PassArray.java -- pt. 2 int a[] = { 1, 2, 3, 4, 5 }; output = "Effects of passing entire " + "array call-by-reference:\n" + "The values of the original array are:\n"; for ( int i = 0; i < a.length ; i++ ) output += " " + a[ i ]; modifyArray ( a ); //passing the whole array output+="\n\nValues of the modified array are:\n";

PassArray.java -- pt. 3 for ( int i = 0; i < a.length ; i++ ) output += " " + a[ i ]; output += "\n\nEffects of passing array " + "element call-by-value:\n" + "a[3] before modifyElement: " + a[ 3 ]; modifyElement ( a [ 3 ] ); output += "\na[3] after modifyElement: " + a [ 3 ]; outputArea.setText ( output ); }

PassArray.java -- pt. 4 public void modifyArray ( int b[] ) { for ( int j = 0 ; j < b.length; j++ ) b [ j ] *= 2; } public void modifyElement ( int e ) { e *= 2; }

Sorting Arrays using Bubble Sort Reorganizing an array in some order (low to high, etc.) Bubble Sort compares two values, switches them in the array positions if appropriate, and checks the next two values

Sorting Arrays using Bubble Sort In this case, 3 & 6 are compared, and NOT switched. Then, 6 & 1 are compared, then switched. Then 6 & 9 are compared and NOT switched. This is ONLY ONE PASS through the array

Sorting Arrays using Bubble Sort Core of sorting: an if structure. This is nested inside a for loop to look at each pair of values in the array. This loop in nested inside another loop to make multiple passes through the array. Look at SortThem & its source code.source code

Searching Arrays: Linear Search How do you find a particular element value in an array? One way: a linear search. The core structure: an if statement in a for loop. The for loop gives movement through the array The if structure looks for a matching value in the elements.

Searching Arrays: Linear Search The for loop gives movement through the array The if structure looks for a matching value in the elements. for ( int n = 0 ; n < array.length ; n++ ) { if ( array [ n ] == key ) return n ; }

Searching Arrays: Binary Sort Go to the middle of the array. See if the number you are looking for is higher or lower, and go to the middle of that side. Repeat until found. Note that the array must already be sorted!

Searching Arrays: Linear vs. Binary A linear search works well for smaller arrays, and is simpler to understand and troubleshoot. A binary search is better for a large array. It is more efficient in its searching.

Program of the Day: BinarySearch pg. 291 BinarySearch.java Once you get it to work, figure out how it works. Next time: review searching and learn about multiple-subscripted arrays.