Slide 6 - COP3502 Programming Fundamentals for CIS Majors 1...

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

View Full Document Right Arrow Icon
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
Background image of page 1

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

View Full DocumentRight Arrow Icon
Chapter 5 Methods Memory management Variable scope Last Week
Background image of page 2
Chapter 6 Arrays Declaration Initialization Access Common operations Summing all entries, max, min, … for -each loop Variable length argument list Linear search, binary search Sorting Objectives
Background image of page 3

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

View Full DocumentRight Arrow Icon
Arrays
Background image of page 4
Read one hundred numbers, compute their average, and find out how many numbers are above the average. Declaring 100 integer variables? Motivation
Background image of page 5

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

View Full DocumentRight Arrow Icon
Better solution: using arrays Array is a data structure that represents a collection of the same type of data. Solution int [] myList = new int [7]; 23 45 53 16 32 8 91
Background image of page 6
Array is a reference type Solution 23 45 53 16 32 8 91 int [] myList = new int [7]; 0x675 myList (memory location of the actual array) Array element at index 6 Value of element at index 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
First approach: Example: Second approach (possible, but not preferred): Example: Declaring Array datatype[] arrayRefVar; double [] myList; datatype arrayRefVar[]; double myList[];
Background image of page 8
Example: Creating Array arrayRefVar = new datatype[arraySize]; //two steps double [] myList; myList = new double [10]; //one step double [] myList= new double [10];
Background image of page 9

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

View Full DocumentRight Arrow Icon
Once an array is created, its size is fixed. i.e. it cannot be changed! You can find the size of an array using For example This returns 7. Length of Array arrayRefVar.length int length = myList.length;
Background image of page 10
When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types ' \u0000 ' for char false for boolean Initial Values
Background image of page 11

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

View Full DocumentRight Arrow Icon
The array elements are accessed through an index. The array indices are 0 -based, i.e., myList indices starts from 0 to 6. Indexed Variables
Background image of page 12
Each element of array is an indexed variable : Example (accessing first element) Indexed Variables arrayRefVar[index]; myList[0];
Background image of page 13

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

View Full DocumentRight Arrow Icon
Individual initialization Populate Array double [] myList = new double [4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;
Background image of page 14
Shorthand initialization This shorthand syntax must be in one statement. Splitting it would cause a syntax error! Populate Array double [] myList = {1.9, 2.9, 3.4, 3.5};
Background image of page 15

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

View Full DocumentRight Arrow Icon
First create an array Populate the array Use indexed variables to access items in the same way as a regular variable. Indexed Variables myList[2]= myList[0] + myList[1];
Background image of page 16
Read one hundred numbers, compute their average, and find out how many numbers are above the average. Program AnalyzeNumbers Run
Background image of page 17

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

View Full DocumentRight Arrow Icon
Program Trace public class Test { public static void main(String[] args) { [] values = new int [5]; for ( int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Declare array variable Values
Background image of page 18
Program Trace public class Test { public static void main(String[] args) { int for ( int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Declare array variable, and create an array.
Background image of page 19

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

View Full DocumentRight Arrow Icon
Image of page 20
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/15/2012 for the course COP 3502 taught by Professor Staff during the Spring '12 term at University of Florida.