Chapter 8 Spring 2006 CS 101 Aaron Bloomfield

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Introduction to arrays Data in economy size packages.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
1 Arrays Chapter 8 Spring 2007 CS 101 Aaron Bloomfield.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with 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.
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.
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.
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.
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.
1 Arrays Chapter 8 Spring 2006 CS 101 Aaron Bloomfield.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui.
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.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
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.
1 Arrays. 2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional 
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
1 Arrays. 2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional 
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Visual Puns 1Vickie C. Ball, Harlan High School. “eggplant” 2Vickie C. Ball, Harlan High School.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Arrays Chapter 7.
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Test 2 Review Outline.
Arrays Chapter 7.
Chapter 7 User-Defined Methods.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 Part 1 Edited by JJ Shepherd
Module 2 Arrays and strings – example programs.
7 Arrays.
Arrays.
Arrays Continued.
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays in Java What, why and how Copyright Curt Hill.
Chapter 8 Fall 2006 CS 101 Aaron Bloomfield
Object Oriented Programming in java
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
Building Java Programs
7 Arrays.
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Outline Declaring and Using Arrays Arrays of Objects
Review for Midterm 3.
Arrays.
A type is a collection of values
Presentation transcript:

Chapter 8 Spring 2006 CS 101 Aaron Bloomfield Arrays Chapter 8 Spring 2006 CS 101 Aaron Bloomfield

Background Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional Java provides arrays and the collection classes The Vector class is an example of a collection class Consider arrays first

Example Definitions char[] c; int[] value = new int[10]; Causes Array object variable c is un-initialized Array object variable value references a new ten element list of integers Each of the integers is default initialized to 0 c can only reference char arrays v can only reference int arrays value - c …

An array example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); Suppose 3 is extracted 8 is displayed int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = stdin.nextInt(); // element k of v given next // extracted value

Array variable definition styles Without initialization ElementType [ ] id; Type of values in list Brackets indicate array variable being defined Name of list int [] a; int a[];

Array variable definition styles With initialization Nonnegative integer expression specifying the number of elements in the array ElementType [ ] id = new ElementType [n]; A new array of n elements

Where we’ve seen arrays public static void main (String[] args) Thus, the main() method takes in a String array as the parameter Note that you can also define it as: public static void main (String args[]) or public static void main (String[] foobar)

Basic terminology List is composed of elements Elements in a list have a common name Example: a[3] = 5; The common name is ‘a’ The list as a whole is referenced through the common name List elements are of the same type — the base type Elements of a list are referenced by subscripting (indexing) the common name

Java array features Subscripts are denoted as expressions within brackets: [ ] Base (element) type can be any type Size of array can be specified at run time This is different that pure C! (for the most part, at least) Index type is integer and the index range must be 0 ... n-1 Where n is the number of elements Just like Strings indexing! Automatic bounds checking Ensures any reference to an array element is valid Data field length specifies the number of elements in the list Array is an object Has features common to all other objects More on this later… More robust than arrays in most programming languages

Consider Segment int[] b = new int[100]; b[-1] = 0; b[100] = 0; Causes Array variable to reference a new list of 100 integers Each element is initialized to 0 Two exceptions to be thrown -1 is not a valid index – too small 100 is not a valid index – too large IndexOutOfBoundsException int[] b = new int[100]; // b has 100 elements: b[0], … b[99] b[-1] = 0; // illegal: subscript too small b[100] = 0; // illegal: subscript too large

Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex; Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex; Point: (1, 0) p p[0] p[1] Point: (2, 2) p[2] vertex Point: (4, 4) Point: (1, 0) p p[0] p[1] p[2] vertex Point: (4, 4) Point: (1, 0) p p[0] p[1] Point: (1, 2) Point: (2, 2) p[2] vertex Point: (4, 4) Point: (1, 0) p p[0] p[1] Point: (1, 1) Point: (2, 2) p[2] Point: (1, 0) p p[0] p[1] Point: (1, 2) Point: (2, 2) p[2] p p[0] p[1] p[2] null Point: (0, 0) p p[0] p[1] Point: (1, 1) Point: (2, 2) p[2]

New 2005 demotivatiors!

Explicit initialization Syntax id references an array of n elements. id[0] has value exp0, id[1] has value exp1, and so on. ElementType id exp , exp , ... exp [] = { } ; 1 n -1 Each expi is an expression that evaluates to type ElementType

Explicit initialization Example String[] puppy = { “pika”, “mila”, “arlo”, “nikki” }; int[] unit = { 1 }; Equivalent to String[] puppy = new String[4]; puppy[0] = “pika"; puppy[1] = “mila"; puppy[2] = “arlo"; puppy[3] = “nikki"; int[] unit = new int[1]; unit[0] = 1;

Array members Member length Size of the array for (int i = 0; i < puppy.length; ++i) { System.out.println(puppy[i]); }

Review of arrays Creating an array: int[] foo = new int[10]; Accessing an array: foo[3] = 7; System.out.print (foo[1]); String[] bar = new String[10]; bar[3] = “qux”; System.out.println (bar[1]);

How Java represents arrays Consider int[] a = { 1, 2, 3, 4, 5 }; + … Array - length = 5 - data = 1 2 3 4 5 1 2 3 4 5 a

More about how Java represents Arrays int[] a; int[] b = null; int[] c = new int[5]; int[] d = { 1, 2, 3, 4, 5 }; a = c; d = c; Consider int[] a; int[] b = null; int[] c = new int[5]; int[] d = { 1, 2, 3, 4, 5 }; a = c; d = c; a - c can only reference char arrays v can only reference int arrays b null c 1 2 3 4 5 d

What do these pictures mean? Light beer Dandy lions Assaulted peanut Eggplant Dr. Pepper Pool table Tap dancers Card shark King of pop I Pod Gator aide Knight mare Hole milk

ArrayTools.java – outline public class ArrayTools { // class constant private static final int MAX_LIST_SIZE = 1000; // sequentialSearch(): examine unsorted list for key public static int sequentialSearch(int[] data, int key) { ... // putList (): prints list to screen public static void putList(int[] data) { ... // getList(): extract and return up to MAX_LIST_SIZE values public static int[] getList() { ... // reverse(): reverses the order of the element values public static void reverse(int[] list) { ... // binarySearch(): examine sorted list for a key public static int binarySearch(char[] data, char key) { ... }

ArrayTools.java method putList() To print the array: public static void putList(int[] data) { for (int i = 0; i < data.length; ++i) { System.out.println(data[i]); } Consider int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; putList(score); Method sequentialSearch() has two formal parameters—the first parameter is an int[] array data and the second parameter is the search value key. Method sequentialSearch() is similar in form to the code segment that searched an array for a key value in Section . However, sequentialSearch() does not display a message indicating whether it found the value. If sequentialSearch() finds the key value in the array, it returns the subscript of the first matching element. If the key value is not among the array element values, sequentialSearch() returns the number of elements in the array. Because the array elements occupy subscript positions 0 through data.length-1 in the array, the value -1 indicates that the key value is not in the array.

ArrayTools.java method getList() public static int[] getList() { Scanner stdin = new Scanner (System.in); int[] buffer = new int[MAX_LIST_SIZE]; int listSize = 0; for (int i = 0; (i < MAX_LIST_SIZE) && stdin.hasNext(); ++i) { buffer[i] = stdin.nextInt(); ++listSize; } int[] data = new int[listSize]; for (int i = 0; i < listSize; ++i) { data[i] = buffer[i]; return data;

ArrayTools.java method reverse() public static void reverse(int[] data) { int[] clone = data.clone(); for ( int i = 0; i < clone.length; ++i ) { data[i] = clone[clone.length-1-i]; } Consider int[] foo = { 1, 2, 3, 4, 5 }; reverse (foo); putList (foo); Method sequentialSearch() has two formal parameters—the first parameter is an int[] array data and the second parameter is the search value key. Method sequentialSearch() is similar in form to the code segment that searched an array for a key value in Section . However, sequentialSearch() does not display a message indicating whether it found the value. If sequentialSearch() finds the key value in the array, it returns the subscript of the first matching element. If the key value is not among the array element values, sequentialSearch() returns the number of elements in the array. Because the array elements occupy subscript positions 0 through data.length-1 in the array, the value -1 indicates that the key value is not in the array.