Download presentation
Presentation is loading. Please wait.
Published bySpencer Harrell Modified over 8 years ago
1
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles
2
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. Fall 2012 CS2302: Programming Principles 2
3
Basic Operations Declaration: – elementType[ ] arrayRefVar; Example: – double[ ] myList; Fall 2012 CS2302: Programming Principles 3
4
Basic Operations Creation: – arrayRefVar = new elementType[arraySize]; Example: – myList = new double[10]; Declaring and Creating in one step: – dobule[ ] myList = new double[10]; Fall 2012 CS2302: Programming Principles 4
5
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 2012 CS2302: Programming Principles 5
6
Basic Operations Assigning values – arrayRefVar[index] = value; Example: – myList[5] = 34.33; Fall 2012 CS2302: Programming Principles 6
7
Basic Operations Array initialization with declaration Declaring, creating, initializing in one step: – double[ ] myList = {1.9, 2.9, 3.4, 3.5}; Fall 2012 CS2302: Programming Principles 7
8
Objectives Pass arrays to methods Return an Array from a method – Reverse Arrays Partially Filled Arrays – Insert into a partially filled array Fall 2012 CS2302: Programming Principles 8
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 2012 CS2302: Programming Principles 9
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 " + 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 10
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 2012 CS2302: Programming Principles 11
12
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] } Simple Example Fall 2012 CS2302: Programming Principles 12
13
Passing Arrays as Arguments Objective: Demonstrate differences of passing primitive data type variables and array variables. Fall 2012 CS2302: Programming Principles 13 Run TestPassArray
14
Objectives Pass arrays to methods Return an Array from a method – Reverse Arrays Partially Filled Arrays – Insert into a partially filled array Fall 2012 CS2302: Programming Principles 14
15
Objectives Pass arrays to methods Return an Array from a method – Reverse Arrays Partially Filled Arrays – Insert into a partially filled array Fall 2012 CS2302: Programming Principles 15
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 Fall 2012 CS2302: Programming Principles 16 Run
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.