int [] scores = new int [10];

Slides:



Advertisements
Similar presentations
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Advertisements

Chapter 10 Introduction to Arrays
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
int [] scores = new int [10];
Week 6 - Friday.  What did we talk about last time?  Loop examples.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Arrays Chapter 7.
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
Sections 10.1 – 10.4 Introduction to Arrays
CSC111 Quick Revision.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Multiple variables can be created in one declaration
While Loops in Python.
Building Java Programs
CSC 142 Computer Science II
Arrays An Array is an ordered collection of variables
Building Java Programs Chapter 7
Chapter 6 Arrays Solution Opening Problem
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
int [] scores = new int [10];
Building Java Programs
Building Java Programs
Object Oriented Programming in java
python.reset() Also moving to a more reasonable room (CSE 403)
elementary programming
Announcements Lab 6 was due today Lab 7 assigned this Friday
Building Java Programs
Single-Dimensional Arrays chapter6
1D Arrays and Lots of Brackets
Dr. Sampath Jayarathna Cal Poly Pomona
Suggested self-checks: Section 7.11 #1-11
In this class, we will cover:
Building Java Programs
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
Building Java Programs
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Random Numbers while loop
int [] scores = new int [10];
How do you do the following?
First Semester Review.
Arrays.
Week 7 - Monday CS 121.
Presentation transcript:

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];

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

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

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.)

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)

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.

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};

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

“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.

“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…”

“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.

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.

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.

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: 1 5 4 5 7 6 8 6 5 4 5 4 smooth: 3 3 4 5 6 7 6 6 5 4 4 4 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

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.