CS102 A Wide Array of Possibilities CS 102 Java’s Central Casting.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
 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.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©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.
 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.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
© 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.
February 11, 1999CSPP503 Week 6 Bring on the Caffeine CSPP503 Week 6.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching 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.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Chapter 6- Arrays. Overview n What are arrays? n Declaring/initializing arrays. n Using arrays. n Arrays details. n Using arrays with classes/ in classes.
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.
 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 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
Arrays Chapter 7.
Chapter 8 Arrays Objectives
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 8 Arrays Objectives
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.
Introduction To Programming Information Technology , 1’st Semester
Topics discussed in this section:
Chapter 8 Arrays Objectives
A Wide Array of Possibilities
Presentation transcript:

CS102 A Wide Array of Possibilities CS 102 Java’s Central Casting

CS102 What’s an Array An array is a group of values of the same type (usually stored in consecutive memory locations) Arrays are objects –Can be assigned to variables of type Object Dynamically created

CS102 An Array of Examples From Figure 5.3 int n[]; // declare an array of integers // initialize instance variables public void init() { n = new int[ 10 ]; //dynamic allocation }

CS102 The Declaration of Array Array declarations int[] ai; // array of int short[][] as;// array of array of short Object[] ao, // array of Object otherAo; // array of Object

CS102 Declaring Arrays Can’t specify the length in a declaration int test [] = new int [100]; // Okay int test2 [100]; // No good int test3 [100]=new int [100]; // No good

CS102 Some Array Definitions These create array objects: Exception ae[] = new Exception[3]; Object aao[][] = new Exception[2][3]; int[] factorial = { 1, 1, 2, 6, 24 }; String[] aas = {"array", "of", "String"};

CS102 An Array Example int face; int frequency[]; // initialize instance variables public void init() { frequency = new int[ 7 ]; for (int roll=1; roll <= 6000; roll++ ) { face = 1 + (int) (Math.random() * 6 ); ++frequency[ face ]; }

CS102 An Array of Observations Arrays know their own length test.length == 100 –What kind of loops do we usually use with arrays? Arrays start at index 0 –Last index == ?? Arrays must be indexed by int values ( short, byte, or char are okay, but long is no good)

CS102 Operating on an Array Square brackets are an operator Brackets have very high precedence –Same as ()’s Example -17*studentGrades[12] * 2

CS102 "Can he do that?" What’s wrong with the following code? int scores[] = {98, 76, 84, 97, 101, 78}; int counter=0; for (; counter <= scores.length; counter++) { System.out.println("Test #" + counter + ": " + scores[counter]); }

CS102 Array Initializers Use {}’s to set initial values –int grades[] = {87, 92, 76, 94}

CS102 Finally, a Constant Variable What’s wrong with the following code? // Figure 5.6 // This Applet won’t compile import java.applet.Applet; public class FinalTest extends Applet { final int x; }

CS102 Pass the Salt, and an Array of int From last time: primitive types and reference types Parameter passing: offering data to a method in the method call public void paint(Graphics graphObject) –When you call paint, give it a Graphics object How does Java pass data around?

CS102 Call Me Ishmael, But Don't Call Me Late for Dinner Call-by-value –The called method gets a copy of the actual data (i.e., its value) –Called method can't corrupt the original data, because it's only got a copy Call-by-reference –The called method gets a reference to the data

CS102 Arrays & Call-by-Reference Reference types are always passed call-by-reference –Pro: Don’t need to copy lots of data –Con: Since you can access the original object, you can change it Arrays are a reference type –Whole arrays are passed call-by-reference

CS102 What About Array Elements? If an array is an array of a reference type, then individual elements are passed as call-by-____________ Graphics[] graphicsArray = new Graphics[10]; If an array is an array of a reference type, then individual elements are passed as call-by-____________ int[] scores = new int[10];

CS102 Searching & Sorting Data The Second Law of Thermodynamics Sorting restores order to the world Sorting in everyday life –Bank checks by account Searching is easier when sorted –Reverse directories of phone numbers

CS102 Sorting in Computers Usually start with an array of items to be sorted –Integers –Social Security numbers –Last names and first names Lots of data –All the SSN’s in the U.S. –All of GM’s employees

CS102 Different Ways to Sort Bubble sort –Compare two neighbors, swap if out of order

CS102 The Code for Bubble Sort public void sort() { int hold; // temporary holding area for swap // passes for ( int pass = 1; pass < a.length; pass++ ) // one pass for ( int i = 0; i < a.length - 1; i++ ) // one comparison if ( a[ i ] > a[ i + 1 ] ) { // one swap hold = a[i]; a[ i ] = a[ i + 1 ]; a[ i + 1 ] = hold; } Nested for loops

CS102 There’s More Than One Way to Sort a Cat Other sorts –Quicksort –Heapsort –Bucket sort Different sorts have different characteristics –Memory usage –Speed

CS102 Searching Here, There & Everywhere Need to find a value? Search for it! Linear search –Is it the next one? No, then look again Yes, stick a fork in me, ‘cuz I’m done –Works on unsorted items Binary search –Cut in half every time

CS102 You’re a Detective in a Small Town The killer called the victim Get the LUDs Look up the number in a reverse directory

CS102 Arrays of Arrays Arrays in multiple dimensions –Not just a list –A table (an array of arrays) –A cube (an array of arrays of arrays)

CS102 An Example An array of arrays int b[][] = { {1,2}, {3, 4, 5}} Row 0:12 Row 1:345 A 3x3 array int b[][]; b = new int[3][3];