8/7: Ch. 7: Arrays What is an array? Declaring & allocating arrays Program of the day.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Chapter 8: Arrays.
Arrays.
Question from the last class What happens if we cast “too large” float/double to an int? int has range float a=1e10f; int b=(int)
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Introduction to arrays Data in economy size packages.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 8: Arrays.
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.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Review for Nested loops & Math class methods & User defined methods.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
8/8: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Arrays Chapter 7.
Array in C# Array in C# RIHS Arshad Khan
3 Introduction to Classes and Objects.
7 Arrays.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Arrays Arrays A few types Structures of related data items
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

8/7: Ch. 7: Arrays What is an array? Declaring & allocating arrays Program of the day

Arrays: what are they? a series of elements; a list. grocery list could be an array: –milk –bread –eggs –frozen pizza –juice –apples –oranges –Ramen noodles

Arrays: what are the positions? every item has a place; a position number. placeitem 1. milk 2. bread 3. eggs 4. frozen pizza 5. juice 6. apples 7. oranges 8. Ramen noodles

Arrays: how are they numbered? Java begins numbering at 0. placeitem 0. milk 1. bread 2. eggs 3. frozen pizza 4. juice 5. apples 6. oranges 7. Ramen noodles

Arrays: how are they named? Java names the list and refers to the place of the items in square brackets after the list name. groceryList [0] =milk groceryList [1]=bread groceryList [2] =eggs groceryList [3] =frozen pizza groceryList [4] =juice groceryList [5] =apples groceryList [6] =oranges groceryList [7] =Ramen noodles

Arrays: what are the elements? The individual items are called elements. The reference numbers are position numbers,or subscripts. groceryList [0] =milk groceryList [1]=bread groceryList [2] =eggs groceryList [3] =frozen pizza groceryList [4] =juice groceryList [5] =apples groceryList [6] =oranges groceryList [7] =Ramen noodles elements subscripts

Arrays: about the position numbers Subscripts must be integers or integer expressions. groceryList [0] =milk groceryList [1]=bread groceryList [2] =eggs groceryList [3] =frozen pizza groceryList [4] =juice groceryList [5] =apples groceryList [6] =oranges groceryList [7] =Ramen noodles

Arrays: function examples in Java an array of integers: list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9 list.length = 7 list [0] + list [2] = 2 list [1 + 3] = 8

Declaring and allocating an array to declare and allocate an array: int list[]; list = new int[7]; or int list[] = new int[7]; Another example: String args[] = new String[5]; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9

Declaring & initializing an array to declare and initialize an array: int list[] = {5,10,-3,-7,8,1,9}; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9

initializing elements of an array to initialize an array element: list[6] = 9; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9

StudentPoll.java: pt. 1 //fig. 7.7: StudentPoll.java import javax.swing.*; public class StudentPoll { public static void main ( String args[] ) { int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8,10, 1, 6, 3, 8, 6,10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8,10}; int frequency[] = new int [ 11 ]; String output = "";

About StudentPoll.java: pt. 2 for ( int a = 0 ; a < responses.length; a++ ) ++frequency [ responses [ a ] ]; output += "Rating\tFrequency\n"; for ( int r = 1; r < frequency.length ; r++ ) output += r + "\t" + frequency[r] + "\n";

About StudentPoll.java: pt. 3 JTextArea outputArea = new JTextArea (11,10); outputArea.setText( output ); JOptionPane.showMessageDialog ( null, outputArea, "Student Poll Program", JOptionPane.INFORMATION_MESSAGE ); System.exit ( 0 ); }

About.length length is actually an instance variable, indicating the number of elements in the array. We dont have to declare it separately – its an automatic part of an array that we declare.

Passing arrays to methods Two ways: –passing the entire array as a whole –passing individual elements of the array The method that modifies the array must have an appropriate input type (parameter). Accepting an array: public void x ( int a[] ) Accepting elements: public void y ( int c ) public void z ( String d )

Getting more experience with arrays Pg 273: InitArray.java – declaring & allocating Pg 274: InitArray.java – initializing to values Pg 275: InitArray.java – using a for loop to set values Pg 276: SumArray.java – using a for loop to add up elements of an array

Programs of the day pg. 278: StudentPoll.java After getting the program to run, modify the program to allow the user to input the array elements. –use a JOptionPane.showInputDialog to get the data from the user, and assign the CONVERTED value you get from there into the next array position. Use a for loop to provide the repetition and movement in the array position. Pg. 284 PassArray.java