Presentation is loading. Please wait.

Presentation is loading. Please wait.

int [] scores = new int [10];

Similar presentations


Presentation on theme: "int [] scores = new int [10];"— Presentation transcript:

1 int [] scores = new int [10];
for (int i=0; i<scores.length; i++) { System.out.println(scores[i]); } scores.length AP Java for (int s : scores) { System.out.println(s); } int [ ] scores = { 10, 50, 20 }; More on Arrays int [] scores = new int [10];

2 Today Review Arrays Declaration Inputting Values Processing Outputting
For..each loop Programming

3 Some Vocabulary Array: A numbered sequence of items, which are all of the same type. Index: The position of the item. In Java they are numbered 0, 1, 2, … (n-1) where n is the number of elements in the array. Base type: the type of elements in the array. Can have arrays of primitives, classes, or interfaces

4 Java arrays are Objects
Created using new The array variable does not hold the array, it holds the reference to the array. (It is a pointer) It is a pointer that is defined in the array declaration. As a reference, it can have a value of null. (Like nil in Pascal.)

5 Declaration: Two steps.
Or one step int [] list = new list [5]; int [] list; Creates a variable of type int[] (something that can point to an array of ints.) The variable now has a value of null. list = new int [5]; Points list to an array with 5 spaces for ints. The length of the array is an instance variable. You can get the length by using the method. list.length. (Generically it is arrayname.length)

6 int [] list = new int [5]; (5) list.length list[0] list[1] list[2]
list.length list[0] list[1] list[2] list[3] list[4] Note: In Java, a newly created array is automatically filled with: 0 for numbers false for boolean Unicode number zero for char null for objects.

7 Declaring and initializing
int [] list = { 1, 4, 9, 16 }; The elements using to initialize the array can be constants, variables, or expressions as long as the type matches. This can only be done in the declaration. However you can do the following later in the program list = new int[ ] { 4, 3, 2 , 12};

8 Some code examples // do any necessary initialization
for (int i = 0; i < A.length; i++) { . . . // process A[i] } double sum; // The sum of the numbers in A. sum = 0; // Start with 0. sum += A[i]; // add A[i] to the sum, for // i = 0, 1, ..., A.length - 1

9 “For Each” Loop Introduced in Java 5
Works both with standard arrays and ArrayLists Convenient for traversing (going through and looking at the values) for (int s : scores) { System.out.println(s); } An iterator is an object that helps to traverse a collection. It has a method next that supplies the next element of the collection. A “For each” loop is a shortcut for an iterator.

10 “For Each” Loop: Example 1
So s will store the value of the current address of the array. The type of the elements int [ ] scores = { ... }; ... int sum = 0; for (int s : scores) { sum += s; } The name of the array. Basically the same as: for (int i = 0; i < scores.length; i++) { int s = scores[i]; sum += s; } Thus the “for each” loop has been added to Java 5 simply for convenience. Read “for each integer s in scores…”

11 “For Each” Loop (cont’d)
You cannot add or remove elements within a “for each” loop. You cannot change elements of primitive data types or references to objects within a “for each” loop. For an array or ArrayList of objects, a “for each” loop supplies a reference to an element. You can change the object it refers to (unless it is immutable), but you cannot change the reference itself.

12 Copying arrays list.length (5) list[0] 1 list[1] 4
9 16 25 What would the following do? list = new int[] { 1, 4, 9, 16, 25 }; int [] b; b = list; How can you copy all of the elements? b = new int[5]; for (int i = 0; i < list.length; i++) b[i] = list[i]; // Copy each item from list to B.

13 Array Program 2 Complete the following. There are three Array Programs.
Write a program that will roll a pair of six-sided dice 1000 times and count and display how often each roll occurs. Also show which roll occurs the most often and which occurs the least often. Push: Compare the results to what should happen statistically. Push: Display the results in a graph. Create a random compliment generator using an array to store the compliments. Use a loop (so the user can be complimented often) have the computer display one of at least 5 random compliments. Push: Look up a Chatbot to include interaction with the user. Ask questions about them so you can give better compliments.

14 3) Smooth Operator An audio signal is sometimes stored as a list of int values. The values represent the intensity of the signal at successive time intervals. Of course, in a program the signal is represented with an array. Often a small amount of noise is included in the signal. Noise is usually small, momentary changes in the signal level. An example is the "static" that is heard in addition to the signal in AM radio. Smoothing a signal removes some of the noise and improves the perceptual quality of the signal. This exercise is to smooth the values in an integer array. Say that the original values are in the array "signal". Compute the smoothed array by doing this: Each value smooth[N] is the average of three values: signal[N-1], signal[N], and signal[N+1]. For the first element of smooth, average the first two elements of signal. For the last element of smooth, average the last two elements of signal. Use integer arithmetic for this so that the values in smooth are integers. Example In interpreting the results, remember that integer division discards the remainder. It does not compute a rounded value. Here is a sample run of the program: signal: smooth: For your program have the computer fill the signal array of 10 integers with random values that range from 15 to 25. Compute the ‘Smoothed’ array Show both arrays

15 Push: Modify it so it will smooth 5 values.
public class Smooth { public static void main ( String[] args ) int[] signal = {5, 5, 4, 5, 6, 6, 7, 6, 5, 4, 1, 4}; int[] smooth // compute the smoothed value for each cell of the array smooth smooth[0] = smooth[ signal.length-1 ] = for ( ) { } // write out the input for ( int j=0; j < smooth.length; j++) { } // write out the result } Partial Code Push: Modify it so it will smooth 5 values. Push: Let the user select the number of values it will smooth. Push: Modify it so that it will ignore ‘spikes’ by throwing out the highest and lowest readings.


Download ppt "int [] scores = new int [10];"

Similar presentations


Ads by Google