Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Programming Logic and Design Sixth Edition
Chubaka Producciones Presenta :.
Arrays.
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
 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.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
1 CS100J 14 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
1 Collections Working with More than One Number or Data Type or Object.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall The index of the first position in an.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
CSE 1201 Object Oriented Programming ArrayList 1.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
IS 350 Arrays.
Arrays, Collections and Repetition Part A – Arrays and Repetition
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
Object Oriented Programming in java
McDonald’s Kalender 2009.
CS2011 Introduction to Programming I Arrays (I)
McDonald’s calendar 2007.
CHAPTER 4: Lists, Tuples and Dictionaries
McDonald’s calendar 2007.
2015 January February March April May June July August September
Presentation transcript:

Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)

Collections Programs often need to work with data for individual members of a group all customers (e.g., a list of names for a form letter) all customers, identified by an ID number (e.g., a table of ID numbers and names) Such groups are described as "collections"

Java Collections There are three kinds of collections in Java: Arrays Ordered Collections Dictionaries (or "hash tables" in Java) All three contain either primitive data types, or object data All three kinds of collections are objects

Which Type of Collection Should Be Used? Type depends on the nature of the problem Key characteristics to determine which type of collection to use: Array: easy to create/use, size can't change Ordered Collection (or Vector): just about as easy to create and use, but more flexible (can grow and shrink) Dictionary (or Hashtable): stores and retrieves values based on a key (efficient and easy to use) We will only use arrays in project assignments

Arrays An array is an object It can hold any data type, or objects The size is set when it is initialized can't make it larger or smaller Only need one variable name to store many different "elements" student [ 0 ]... student [ 19 ] //variable name followed by [index #]

Declaring Arrays Can put brackets in either of 2 positions int [ ] daysInMonth; // [ ] before identifier String monthNames [ ] ; // [ ] after " Choice is a matter of style James Gosling (author of Java) puts before textbook also uses before Patrick Naughton (another Java author) puts it after just be consistent!

Allocating an Array Like creating any other object Declaring: int [ ] daysInMonth; // only 1 [ ] Allocating memory: daysInMonth = new int [ 12 ]; // only 1 [ ] Or can declare and allocate in 1 line: String monthNames [ ] = new String[ 12 ]; // note 2 [ ], 1 on each side of =

Array Subscripts Items in an array are identified by an index number or "subscript" monthNames = new String [ 12 ]; monthNames [ 0 ] = "January"; monthNames [ 11 ] = "December"; Subscript starts at 0, and goes to 1 less than the size of the array Can use a loop counter as subscripts

Array Subscripts - 2 Can use variables for subscripts, or even calculate the subscripts for (int i = 0; i < 6; i++) // half year { System.out.println(month[i]); System.out.println(month[i + 2]); } variables (or calculated values) must be integers

"Populating" Arrays Can set values individually int daysInMonth [ ] = new int [ 12 ]; daysInMonth [ 0 ] = 31; daysInMonth [ 1 ] = 28; daysInMonth [ 11 ] = 31;

"Populating" Arrays - 2 Can use data to define size and populate: int [ ] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; And: String [ ] monthNames = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

Using Loops With Arrays int [ ] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 10, 31 }; int sum = 0; for (int ctr =0; ctr < 12; ctr++) { sum += daysInMonth[ctr]; }

Practice Write a for loop to load the values 3, 4, and 5 into the following array int myNumbers [ ] = new int [ 3 ]; Syntax of for statement for (init-expr; bool-expr; increment-expr) statement-1

Parallel Arrays Can use multiple arrays, identifying corresponding elements by subscripts monthNames [ 0 ] is "January" etc. daysInMonth [ 0 ] is 31 etc. for (int i = 0; i < 12; i++) System.out.println(monthNames[i] + " " + daysInMonth[i];

Array Data as Arguments can pass array elements to methods that take individual data items as arguments // calling method: printDays(daysInMonth[2]); // method declaration: public static void printDays( int i )

Array Data into Methods - 2 can pass whole arrays into methods that take arrays as parameters (loops inside) int [ ] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 10, 31 }; printDays(daysInMonth); // don't use [ ] public static void printDays(int[ ] numbers)

Array Length If array size is determined by initialization, or array is passed into a method or class, you may not know the length Examples: int [ ] myData = { 28, 11, 40, 51, 10, 31}; can count to determine that size = 6 myMethod(someArray); array size = ?

Array Length - 2 Length can be obtained from a public array class variable as follows: int sizeMonths = monthNames.length; int sizeDays = daysInMonth.length; Recommendation: use this class variable, rather than a constant, in most situations makes maintenance easier and safer

Practice Write a for loop that prints all the elements in an array named myBigArray use its length property to determine how many times to go through the loop Now put this for loop into a method that receives an array as its argument what do you need to add? is there anything else you might want to add or do?