Chapter 6 Review

advertisement
Chapter 6 Arrays
1
Fall 2012 CS2302: Programming Principles
Why array?

Store a large number of values during the
execution of a program.

What is array?
–
2
A fixed-size sequential collection of elements of
the same type.
Fall 2012 CS2302: Programming Principles
Basic Operations

Declaration:
–

Example:
–
3
elementType[ ] arrayRefVar;
double[ ] myList;
Fall 2012 CS2302: Programming Principles
Basic Operations

Creation:
–

Example:
–

myList = new double[10];
Declaring and Creating in one step:
–
4
arrayRefVar = new elementType[arraySize];
dobule[ ] myList = new double[10];
Fall 2012 CS2302: Programming Principles
Basic Operations

Element identification:
–

Example:
–

myList[9]; // represents the last element in the array myList.
Index range:
–
5
arrayRefVar[index];
From 0 to arrayRefVar.length - 1
Fall 2012 CS2302: Programming Principles
Basic Operations

Assigning values
–
arrayRefVar[index] = value;
double[] myList = new double[10];

Example:
–
myList
reference
myList[5] = 34.33;
Array reference
variable
Array element at
index 5
6
Fall 2012 CS2302: Programming Principles
myList[0]
5.6
myList[1]
4.5
myList[2]
3.3
myList[3]
13.2
myList[4]
4
myList[5]
34.33
myList[6]
34
myList[7]
45.45
myList[8]
99.993
myList[9]
11123
Element value
Basic Operations

Array initialization with declaration

Declaring, creating, initializing in one step:
–
7
double[ ] myList = {1.9, 2.9, 3.4, 3.5};
Fall 2012 CS2302: Programming Principles
Objectives

Pass arrays to methods

Return an Array from a method
–

Partially Filled Arrays
–
8
Reverse Arrays
Insert into a partially filled array
Fall 2012 CS2302: Programming Principles
Pass by value
Most methods are passed arguments when they are called. An
argument may be a constant or a variable.
 Math.sqrt(33)




9
Math.sqrt(x)
If a variable is passed, the method receives a copy of the
variable's value. The value of the original variable cannot be
changed within the method.
 because the method only has a copy of the value; it does
not have access to the original variable.
This process is called pass by value.
Fall 2012 CS2302: Programming Principles
Pass by Value
10
public class TestPassByValue {
/** Main method */
public static void main(String[] args) {
// Declare and initialize variables
int num1 = 1;
int num2 = 2;
System.out.println("Before invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
// Invoke the swap method to attempt to swap two variables
swap(num1, num2);
System.out.println("After invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
}
/** swap method - Swap two variables */
public static void swap(int n1, int n2) {
System.out.println("\tInside the swap method");
System.out.println("\t\tBefore swapping n1 is " + n1
+ " n2 is " + n2);
// Swap n1 with n2
int temp = n1;
n1 = n2
n2 = temp;
System.out.println("\t\tAfter swapping n1 is " + n1
+ " n2 is " + n2);
}
}
Fall 2012 CS2302: Programming Principles
Passing Arrays to Methods
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
//Invoke the method
int[] list = {3, 1, 2, 6, 4, 2};
//initialize array
printArray(list);
//Invoke the method
printArray(new int[]{3, 1, 2, 6, 4, 2});
Anonymous array
11
Fall 2012 CS2302: Programming Principles
Simple Example
public class Test {
public static void main(String[] args) {
int x = 1; // x represents an int value
int[] y = new int[10]; // y represents an array of int values
m(x, y); // Invoke m with arguments x and y
System.out.println("x is " + x);
System.out.println("y[0] is " + y[0]);
}
public static void m(int number, int[] numbers) {
number = 1001; // Assign a new value to number
numbers[0] = 5555; // Assign a new value to numbers[0]
}
}
12
Fall 2012 CS2302: Programming Principles
Passing Arrays as Arguments

Objective: Demonstrate differences
of passing primitive data type
variables and array variables.
TestPassArray
Run
13
Fall 2012 CS2302: Programming Principles
Objectives

Pass arrays to methods

Return an Array from a method
–

Partially Filled Arrays
–
14
Reverse Arrays
Insert into a partially filled array
Fall 2012 CS2302: Programming Principles
Objectives

Pass arrays to methods

Return an Array from a method
–

Partially Filled Arrays
–
15
Reverse Arrays
Insert into a partially filled array
Fall 2012 CS2302: Programming Principles
Ending a Loop with a Sentinel Value
Often the number of times a loop is executed is
not predetermined. You may use an input value
to signify the end of the loop. Such a special input
values is known as a sentinel value.
Write a program that reads and calculates the
sum of an unspecified number of integers. The
input 0 signifies the end of the input.
SentinelValue
Run
16
Fall 2012 CS2302: Programming Principles
Download