1 CS1110 28 Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.

Slides:



Advertisements
Similar presentations
Months of the year December January November October February
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Chubaka Producciones Presenta :.
CS102 A Wide Array of Possibilities CS 102 Java’s Central Casting.
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
1 CS100J 14 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Java Unit 9: Arrays Declaring and Processing Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter 9: Advanced Array Concepts
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.
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
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.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 CS Nov 2011 Ragged arrays Reading for today: sec Reading for next time: Interfaces: Sec and corresponding ProgramLive material. Also,
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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];
1 CS100J 13 October 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. Please listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Java Language Basics.
Java Classes and Objects 3rd Lecture
McDonald’s calendar 2007.
Arrays in Java.
Asymptotic complexity
CS November 2010 Developing array algorithms. Reading:
McDonald’s calendar 2007.
2015 January February March April May June July August September
Presentation transcript:

1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and each has an insightful message. 1. The 3 lectures on Lesson page 7-6 —read the whole page. 2. The 4 lectures in Lesson page 7-5. Computational simplicity If you are writing too much code —it gets longer and longer, with no end in sight: stop and look for a better way. If your code is getting convoluted and you have trouble understanding it: stop and look for a better way. Learn to keep things simple, to solve problems in simple ways. This sometimes requires a different way of thinking. We are trying to teach not just Java but how to think about problem solving. A key point is to break a problem up into several pieces and do each piece in isolation, without thinking about the rest of them. Our methodology for developing a loop does just that.

2 Array: object. Can hold a fixed number of values of the same type. Array contains 4 int values a0 Basic form of a declaration: ; A declaration of x. Does not create array, it only declares x. x’s initial value is null. int[] x ; Elements of array are numbered: 0, 1, 2, …, x.length–1; The type of the array: int[] Variable contains name of the array. x a0 int[] Make everything as simple as possible, but no simpler. Einstein

3 Notes on array length Array length: an instance field of the array. This is why we write x.length, not x.length( ) a Length field is final: cannot be changed. Length remains the same once the array has been created. We omit it in the rest of the pictures. x a0 int[] length 4 The length is not part of the array type. The type is int[] An array variable can be assigned arrays of different lengths.

4 Arrays int[] x ; x null int[] a0 x= new int[4]; Create array object of length 4, store its name in x x a0 int[] a Assign 2*x[0], i.e. -8, to x[3] Assign 6 to x[2] int k= 3; x[k]= 2* x[0]; x[k-1]= 6; x[2]= 5; x[0]= -4; a Assign 5 to array element 2 and -4 to array element 0 x[2] is a reference to element number 2 of array x

5 Difference between Vector and array Declaration: int[] a; Vector v; Elements of a: int values Elements of v: any Objects Array always has n elements Number of elements can change Creation: a= new int[n]; v= new Vector(); Reference: a[e] v.get(e) Change element: a[e]= e1; v.set(e, e1); Array locations a[0], a[1], … in successive locations in memory. Access takes same time no matter which one you reference. Elements all the same type (a primitive type or class type) Can’t tell how Vectors are stored in memory. Referencing and changing elements done through method calls Elements of any Object type (but not a primitive type). Casting may be necessary when an element is retrieved.

6 Array initializers Instead of int[] c= new int[5]; c[0]= 5; c[1]= 4; c[2]= 7; c[3]= 6; c[4]= 5; Use an array initializer: int[] c= new int[ ] {5, 4, 7, 6, 5}; a0 array initializer: gives values to be in the array initially. Values must have the same type, in this case, int. Length of the array is the number of values in the list No expression between brackets [ ]. (can omit this) Computer science has its field called computational complexity; mine is called computational simplicity. Gries

7 Use of an array initializer public class D { public static final String[] months= new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; /** = the month, given its number m Precondition: 1 <= m <= 12 */ public static String theMonth(int m) { return months[m–1]; } Variable months is: static; object assigned to it will be created only once. public: can be seen outside class D. final: it cannot be changed. Months[m–1] is returned, since months[0] = “January”, months[1] = “February”, …

8 a b Procedure swap public class D { /** = Swap x and y */ public static void swap (int x, int y) { int temp= x; x= y; y= temp; } …. swap(a, b); 53 A call will NOT swap a and b. Parameters x and y are initialized to the values of a and b, and thereafter, there is no way to change a and b. swap: 1? x y temp 53 ? frame for call just after frame created and args assigned to pars:

9 Procedure swap a0 c public class D { /** = Swap b[h] and b[k] */ public static void swap (int[] b, int h, int k) { int temp= b[h]; b[h]= b[k]; b[k]= temp; } …. swap(c, 3, 4); Does swap b[h] and b[k], because parameter b contains name of the array. swap: 1? b h temp a03 ? frame for call just after frame is created. k 4

10 // if c is in b[h..k-1], return its index in b[h..k-1] // { c is not in b[h..k-1] } return k; Linear search public class D { /** = index of first occurrence of c in b[h..k-1] — = k if c is not in b[h..k-1]*/ public static int findFirst (int c, int[] b, int h, int k) { } if (b[t] == c) return t; for (int t= h; t < k; t= t+1) { // Process t; } // { invariant: c is not in b[h..t-1] } Remember h..h-1 is the empty range

11 Type of d is int[][] (“int array array”, “an array of int arrays”) To declare variable d: int d[][]. To create a new array and assign it to d: d= new int[3][4]; To reference element at row r column c: d[r][c] number of rows number of cols Two-dimensional arrays b b.length one-dimensional array d rectangular array: 5 rows and 4 columns