Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.

Slides:



Advertisements
Similar presentations
Two Dimensional Arrays and ArrayList
Advertisements

Aalborg Media Lab 2-May-15 Exercises/Summary Lecture 10 Summary, Exercises.
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.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 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.
Multiple-Subscripted Array
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
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.
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
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.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
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.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
COMP More About Arrays Yi Hong June 05, 2015.
int [] scores = new int [10];
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];
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays.
Two-Dimensional Arrays
int [] scores = new int [10];
Multidimensional Arrays
CISC181 Introduction to Computer Science Dr
Arrays I Handling lists of data.
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
Arrays Arrays A few types Structures of related data items
Presentation transcript:

Arrays

Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about next semester?

Arrays Declare a list of variables – all with the same type Size is determined at time of declaration double[] scores = new double[14];

Example scores:

Example //declare the array double[] scores = new double[3]; //put 90.5 in the first box scores:

Example //declare the array double[] scores = new double[3]; //put 90.5 in the first box scores[0] = 90.5; 90.5scores:

Example //declare the array double[] scores = new double[3]; //put 90.5 in the first box scores[0] = 90.5; scores[1] = 73; scores[3] = 87; scores:

Alternative double[] scores = {90.5, 73, 87}; Initializes elements of the array to values given when array is created

Subscripts Subscript describes which box of the array you are dealing with Array of size N has subscripts –0, 1, 2, … (n-1) –array of size 3 – 0, 1, 2 –subscript added to base address of array Subscript can be a variable or expression which produces an integer –scores[i]; –scores[i+5]; If subscript specified does not exist – runtime error (ArrayIndexOutOfBoundsException)

Example //assume i = -2; sum = scores[1] + scores[2]; sum = scores[1] + scores[i*8]; scores[2] = scores[1] + 6; scores:

Accessing Array Elements Print all elements of the array scores –Use only one System.out.println statement

Accessing Array Elements Print all elements of the array scores –Use only one System.out.println statement double[] scores = {90.5, 73, 82}; for(int i = 0; i < 3; i++) { System.out.println("Score " + (i+1) + ": " + scores[i]); }

foreach double[] scores = {90.5, 73, 82}; for(double d : scores) { System.out.println("Score " + d); }

Passing Array Elements Write a method to add two elements of an array

Passing Array Elements Write a method to add two elements of an array –How is the method called? double add(double num1, double num2) { return (num1 + num2); }

Passing Array Elements Write a method to add two elements of an array –How is the method called? add(scores[0], scores[1]); … double add(double num1, double num2) { return (num1 + num2); }

Passing Arrays Would like to pass an entire array into a method –Sum all elements, prompt user for values

Example sum(scores); static double sum(double[] scores) { double sum = 0; for(double d: scores) { sum+=d; } return sum; }

Arrays of objects Flight[] flights = new Flight[10];

Multidimensional Arrays double[][] warmups = new double[14][30]; Declares a multidimensional array with 14 rows and 30 columns …

Example Set first warmup score for first student to 3 …

Example Set first warmup score for first student to 3 warmups[0][0] = 3; …

Example Print all scores for all students …

Example Print all scores for all students static void printscores(double[][] scores) { for(int row = 0; row < scores.length; row++) { for(int col = 0; col < scores[row].length; col++) { System.out.println("Item " + scores[row][col]); }

Exercises 1.Modify your Flight class to keep track of a list (array) of passengers. Provide methods to add new passengers and to print the passenger list.

ArrayList Like an array, but it can dynamically change size Stores object references –cannot store primitive types without a wrapper By default, not declared to store a particular type –ArrayList al = new ArrayList(); Because it is a generic type, we can explicitly specify the type –ArrayList stringal = new ArrayList ();

Exercises 1.Modify your Flight class to use an ArrayList for the passengers rather than an array.