Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.

Similar presentations


Presentation on theme: "Chapter 6 Arrays Fall 2012 CS2302: Programming Principles."— Presentation transcript:

1 Chapter 6 Arrays Fall CS2302: Programming Principles

2 Store a large number of values during the execution of a program.
Why array? Store a large number of values during the execution of a program. What is array? A fixed-size sequential collection of elements of the same type. The Open Systems Interconnection (OSI) model is a product of the Open Systems Interconnection effort at the International Organization for Standardization (ISO). Fall CS2302: Programming Principles

3 Declaration: Example: Basic Operations elementType[ ] arrayRefVar;
double[ ] myList; Fall CS2302: Programming Principles

4 Declaring and Creating in one step:
Basic Operations Creation: arrayRefVar = new elementType[arraySize]; Example: myList = new double[10]; Declaring and Creating in one step: dobule[ ] myList = new double[10]; Fall CS2302: Programming Principles

5 Element identification:
Basic Operations Element identification: arrayRefVar[index]; Example: myList[9]; // represents the last element in the array myList. Index range: From 0 to arrayRefVar.length - 1 Fall CS2302: Programming Principles

6 Assigning values Example: Basic Operations arrayRefVar[index] = value;
myList[5] = 34.33; Fall CS2302: Programming Principles

7 Array initialization with declaration
Basic Operations Array initialization with declaration Declaring, creating, initializing in one step: double[ ] myList = {1.9, 2.9, 3.4, 3.5}; Fall CS2302: Programming Principles

8 Objectives Pass arrays to methods Return an Array from a method
Reverse Arrays Partially Filled Arrays Insert into a partially filled array Fall CS2302: Programming Principles

9 Pass by value Most methods are passed arguments when they are called. An argument may be a constant or a variable. Math.sqrt(33) 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 CS2302: Programming Principles

10 Pass by Value 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 " + n " n2 is " + n2); // Swap n1 with n2 int temp = n1; n1 = n2 n2 = temp; System.out.println("\t\tAfter swapping n1 is " + n " n2 is " + n2); } } Fall CS2302: Programming Principles

11 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 Fall CS2302: Programming Principles

12 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] Fall CS2302: Programming Principles

13 Passing Arrays as Arguments
Objective: Demonstrate differences of passing primitive data type variables and array variables. TestPassArray Run Fall CS2302: Programming Principles

14 Objectives Pass arrays to methods Return an Array from a method
Reverse Arrays Partially Filled Arrays Insert into a partially filled array Fall CS2302: Programming Principles

15 Objectives Pass arrays to methods Return an Array from a method
Reverse Arrays Partially Filled Arrays Insert into a partially filled array Fall CS2302: Programming Principles

16 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 Fall CS2302: Programming Principles


Download ppt "Chapter 6 Arrays Fall 2012 CS2302: Programming Principles."

Similar presentations


Ads by Google