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.

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.
Kernighan/Ritchie: Kelley/Pohl:
Chubaka Producciones Presenta :.
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
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 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.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
1 Collections Working with More than One Number or Data Type or Object.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 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.
Java Unit 9: Arrays Declaring and Processing Arrays.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Calendar for 2011 Months of the Year with Holidays.
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.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
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.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to 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.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
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.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
Advanced Programming Behnam Hatami Fall 2017.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
Year 2 Autumn Term Week 12 Lesson 1
Java Classes and Objects 3rd Lecture
McDonald’s Kalender 2009.
2300 (11PM) September 21 Blue line is meridian..
McDonald’s calendar 2007.
Year 2 Autumn Term Week 12 Lesson 1
Arrays in Java.
Teacher name August phone: Enter text here.
MONTHS OF THE YEAR January February April March June May July August
McDonald’s calendar 2007.
Habitat Changes and Fish Migration
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Presentation transcript:

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 of computational complexity. Mine is computational simplicity. Gries The test tonight is in the Uris Auditorium at 7:30 Over the break, please listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and each has an insightful message for you. 1. The three lectures on Lesson page in fact, read the whole page. 2. The four lectures in Lesson page 7-5. It also will help to read Secs. 8,1, 8.2, and 8.3 on arrays.

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. Learning to keep things simple, to solve problems in a simpler way, 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 methdology for developing a loop does just that. Make everything as simple as possible, but no simpler. Einstein

Arrays An array is an object that can hold a fixed number of values of the same type. The array to the right contains 4 int values a0 Remember that a basic declaration has the form ; Therefore, a declaration of x looks as to the right. The declaration does not create the array, it only declares x. x’s initial value is null. We’ll show you later how to create the array. int[] x ; The elements of the array are numbered 0, 1, 2, …, x.length–1. Note that length is a variable, not a function, so don’t put () after it. The type of the array to the right is int[] Here is a variable that contains the name of the array. x a0 int[]

Arrays int[] x ;x null int[] a0 x= new int[4]; Create an array object of length 4 and 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

Difference between Vector and array --both used to contain a bunch of things 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], a[2] are in successive location in memory. Access is guaranteed to be the same, no matter which one you reference. Elements are all the same type (a primitive type or some Object type) You can’t tell how Vectors are stored in memory. Referencing and changing elements done through method calls Elements can be of any Object type (but not a primitive type), and casting may be necessary when an element is retrieved.

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 the values to be in the array initially. The values must all have the same type, in this case, int. The length of the array is the number of values in the list No expression between the brackets [ ].

A use of an array initializer public class D { private static 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 made static, so that the object assigned to it will be created only once. It is private, so that it cannot be seen outside class D. Note that months[m–1] is returned, since months[0] = “January”, months[1] is “February”, …

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 The 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 is created.

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); This method does swap b[h] and b[k], because parameter b contains the name of the array. swap: 1? b h temp a03 ? frame for call just after frame is created. k 4