lecture12-arrays+notes

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

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

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
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
Ask a homework question - tutors are online