CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 102 Computers In Context (Multimedia)‏ 04 / 20 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 18 / 2006 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
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.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
BIT115: Introduction to Programming
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
int [] scores = new int [10];
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 08 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays An Array is an ordered collection of variables
Comparing Python and Java
Presentation transcript:

CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? solve problem 2 from lab solve problem 3 from lab arrays

Arrays Oftentimes we find ourselves needing to store a list of items (e.g. a list of ages, grades, names, etc.)‏ Up until now, if we wanted to store all the titles of each course at Skidmore in a separate variable, we would need to declare hundreds of differently named variables of type String. This would get out of hand quickly. Fortunately there's a data structure called an array that allows us to store a list of items of the same type. Michael Eckmann - Skidmore College - CS Spring 2008

Arrays A data structure of related data items of the same type They store this data in contiguous memory locations Each data item in an array is an element. Arrays are denoted with square brackets. When you want to select one element of the array, use its index in square brackets. Array indices start at 0. When we speak of arrays: array means the full array, and array element is one element of the array. Michael Eckmann - Skidmore College - CS Spring 2008

Arrays Arrays can be of any type (user-defined class types, Java API class types, or primitive types.)‏ Arrays of primitive types: –Might use an array of integers to store test scores. (better than creating individual variables of type int for reasons which should become clear.)‏ –Might use an array of doubles to store a list of temperatures. Arrays of Java API class types: –Might use an array of Strings to store a list of people’s names. Michael Eckmann - Skidmore College - CS Spring 2008

Arrays (declaring and allocating)‏ Arrays, like any variables need to be declared. Arrays also need to have memory allocated for a specific number of elements. Example: int test_scores[]; // declares an array of ints test_scores = new int[ 16 ]; // the line above allocates enough memory for 16 ints Michael Eckmann - Skidmore College - CS Spring 2008

Arrays (declaring and allocating)‏ Arrays can be declared and allocated in one line. Example: // declares an array of ints & allocates enough memory for 16 ints int test_scores[] = new int[ 16 ]; Michael Eckmann - Skidmore College - CS Spring 2008

Arrays (allocating and initializing)‏ When arrays are allocated, the array elements are all initialized to the following values: Arrays of numeric primitive types (byte, short, long, int, float, double, char) have their elements initialized to 0. Arrays of booleans (the only other primitive type) have their elements initialized to false. Arrays of any non-primitive data type, (classes in the Java API or user-defined classes) have their elements initialized to null. Michael Eckmann - Skidmore College - CS Spring 2008

Arrays (declaring, allocating and initializing)‏ Arrays can be declared, allocated and initialized to user defined values all in one line. Example: int test_scores[] = { 130, 120, 111, 44, 181, 183, 160, 165, 148, 147, 146, 153, 139, 150, 153, 122 }; // declares an array of ints & allocates enough memory for 16 ints and // initializes each element to the value listed Michael Eckmann - Skidmore College - CS Spring 2008

Arrays In our example of the test_scores array, each element of this array is an int. So, we can refer to say the 10th element of the array by test_scores[9] and this is an int, so we can use it anywhere an int can be used. REMEMBER: Array indices start at 0 (zero) and end at the (size of the array – 1). –E.g. Indices for an array of size 36, go from 0 to 35. Array values can be set one element at a time by supplying the index like so: // stores the value 130 in the 0 element of the array test_scores test_scores[0] = 130; // stores the value 120 in the 1 element of the array test_scores test_scores[1] = 120; // etc … Michael Eckmann - Skidmore College - CS Spring 2008

Arrays Each array knows its own length, use the array name followed by.length to get access to its length. In our case for the test_scores example, the length is 16 (0 to 15.)‏ Arrays are easily manipulated with loops. Let’s look at an example. Michael Eckmann - Skidmore College - CS Spring 2008

Array code example // Code to populate (store values into the elements of) an array int test_scores[] = new int[ 16 ]; String user_input; for (int i=0; i < test_scores.length; i++)‏ { user_input = JOptionPane.showInputDialog( “Enter an integer”); test_scores[i] = Integer.parseInt(user_input); } Michael Eckmann - Skidmore College - CS Spring 2008

Array code example // Code to find the lowest element of an array of ints. int least = test_scores[0]; for (int i=0; i < test_scores.length; i++)‏ { if (test_scores[i] < least)‏ least = test_scores[i]; } System.out.println("The lowest value in the array is: " + least); Michael Eckmann - Skidmore College - CS Spring 2008

Array code example Let's write a program to find the average test score. What info do we need to compute the average score? Michael Eckmann - Skidmore College - CS Spring 2008