{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

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

View Full Document
Chapter 5 Methods Memory management Variable scope Last Week
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

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

View Full Document
Arrays
Read one hundred numbers, compute their average, and find out how many numbers are above the average. Declaring 100 integer variables? Motivation

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

View Full Document
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
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

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

View Full Document
First approach: Example: Second approach (possible, but not preferred): Example: Declaring Array datatype[] arrayRefVar; double [] myList; datatype arrayRefVar[]; double myList[];
Example: Creating Array arrayRefVar = new datatype[arraySize]; //two steps double [] myList; myList = new double [10]; //one step double [] myList= new double [10];

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

View Full Document
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;
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

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

View Full Document
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
Each element of array is an indexed variable : Example (accessing first element) Indexed Variables arrayRefVar[index]; myList[0];

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

View Full Document
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;
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};

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

View Full Document
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];
Read one hundred numbers, compute their average, and find out how many numbers are above the average. Program AnalyzeNumbers Run

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

View Full Document
Program Trace public class Test { public static void main(String[] args) { int [] 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
Program Trace public class Test { public static void main(String[] args) { int [] 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, and create an array.

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.

{[ snackBarMessage ]}

### Page1 / 82

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

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

View Full Document
Ask a homework question - tutors are online