lecture12-arrays+notes

lecture12-arrays+notes - cture 12 Arrays Lecture 12 Arrays...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cture 12: Arrays Lecture 12: Arrays CS 170, Section 000 October 2009 8 October 2009 10/9/2009 CS170, Section 000, Fall 2009 1 Lecture Plan ¡ Midterm 1 statistics ¡ Variables, methods review (Ex: P3 from midterm) ¾ Arrays (Ch. 6.1-4, 6.7) ¾ Homework 3 overview (due Wed Oct 14 th ) ¾ Last 5 minutes of class: Midterms returned, questions? 10/9/2009 CS170, Section 000, Fall 2009 2 Example (P3 from Midterm) class Test{ public static void main (String args){ Stack int a=40; int b=20; int avg; System.out.println(“a=” + a + “ b=” + b); avg=Average(a, b); ain: Average: int a=20; int b=10 System.out.println(“a=”+ a + “ b=” + b+ “avg=” + avg); } main: int a=40; int b=20 int avg; public static int Average(int a, int b){ a = a/2; b = b/2; return a+b; } } 10/9/2009 CS170, Section 000, Fall 2009 3 Board and Props Example main: int a=40; int b=20 int avg; Average: int a=20; int b=10 10/9/2009 CS170, Section 000, Fall 2009 4 Chapter 6: Arrays String b1=“Babe Ruth”; tring b2=“Cy Young”; String b2= Cy Young ; String b3=“Lou Gehrig”; String b4=“Hank Aaron”; String b5=“Frank Robinson”; 10/9/2009 CS170, Section 000, Fall 2009 5 … Motivation (cont’d) • Suppose we want to write a program that mputes the average midterm score of all the computes the average midterm score of all the students and find out how many scored above e average the average • How many variables do we need for storing the ores? (30 students!) scores? (30 students!) • What if some students drop the class? (27 now!) 6 • Solution: Arrays Arrays • Array is a data structure that represents a llection of the same types of data collection of the same types of data. • How to use arrays? – Declaring array variables – Creating arrays – Initializing arrays – Accessing and using array elements – Traversing arrays using for loops Declaring Array Variables • datatype arrayRefVar; Preferred! ample: Example: double myList; • datatype arrayRefVar; Example: 8 double myList; Creating Arrays arrayRefVar = new datatype[arraySize]; Example: int a; int a = new int[12]; 9 Declaring and Creating an Array in One Step atatype arrayRefVar; Datatype arrayRefVar; arrayRefVar = new datatype[arraySize]; datatype arrayRefVar = ew datatype[arraySize]; new datatype[arraySize]; ample: Example: double myList = new double[10]; 10 Default Values • When an array is created, its elements are signed the default value: assigned the default value: – for the numeric primitive data types h )0 f – (char)0 for char types – false for boolean types 11 ssigning Values to Array Elements Assigning Values to Array Elements arrayRefVar[index] = expression ; Example: myList[0] = 5.6; yList[1] = 4 5; myList[1] = 4.5; … myList[9] = 11123; 12 Array Initializers - Declaring, creating, initializing an array using one statement double myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the llowing statements following statements: double myList = new double[4];...
View Full Document

{[ snackBarMessage ]}

Page1 / 55

lecture12-arrays+notes - cture 12 Arrays Lecture 12 Arrays...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online