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.

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

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Arrays.
Introduction to arrays Data in economy size packages.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Class design. int month; int year class Month Defining Classes A class contains data declarations (state) and method declarations (behaviors) Data declarations.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
The Java Programming Language
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
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.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
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.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter 7 Arrays: Part 1 of 2. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of.
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
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.
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.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
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];
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Lecture 5 array declaration and instantiation array reference
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.
Introduction to Computing Using Java
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.
Lecture 18 Arrays and Pointer Arithmetic
Arrays in Java.
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Corresponds with Chapter 5
Arrays.
Presentation transcript:

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 group exam marks for students in a subject items in a catalogue orders for gifts We need a better technique than creating a variable for each value or object.

Arrays An array in Java is a collection of values of the same type. The elements in an array are held in contiguous memory locations. The array class is special. It has a small number of predefined methods, and a special notation of its own to invoke them.

Logical View of an Array An array is like a street with houses that can hold things. The houses are numbered so that they can be accessed. In some languages (including Java), the first house number is zero. In other languages, the numbering starts from 1. The number of houses is fixed when the array is created, and cannot be changed without recompiling the program

Declaring & Initializing an Array One way to create an array with some values in it is to initialize it when it is created. In this case, the compiler will make the array as big as it needs to be to hold the values specified. int[] marks = {10, 45, 67, 88, 50}; marks

5 Examples of Declaring Arrays float[] prices = new float[500]; int[] marks = {10, 45, 67, 88, 50}; boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750]; char[] letterGrades = {'A', 'B', 'C', 'D', 'F'};

6 Array Declarations Revisited The brackets of the array type can be associated with the element type or with the name of the array Therefore the following declarations are equivalent: float[] prices; float prices[]; The first format is generally more readable

Command-Line Arguments The signature of the main method indicates that it takes an array of String objects as a parameter These values come from command-line arguments that are provided when the interpreter is invoked For example, the following invocation of the interpreter passes an array of three String objects into main: > java DoIt pennsylvania texas california These strings are stored at indexes 0-2 of the parameter See NameTag.java (page 281) NameTag.java

Memory Allocation for Array marks[0] marks[1] marks[2] marks[3] marks[4] Array accessMemory mapAddress marks 1000

Accessing an Array An element is accessed by specifying its position in the array: arrayName[index] where index is a value (of type int ) used to select a particular element in the array. The lowest valid index is zero, and the highest is (size of the array) - 1.

Evaluating an Index Any expression that evaluates to an int can be used as an index (also called a subscript). Given the following definitions: int subscript = 0; int[] marks = new int[10]; we could access elements by expressions such as: marks[0] marks[subscript + 2] marks[10-1] An index that is outside the array will cause an ArrayIndexOutOfBoundsException error message.

Assigning Values to an Array You can assign values to particular array elements. int counter = 1; marks[4] = 18; marks[0] = 56; marks[counter] = 38; marks[counter + 1] = 87;

Extracting Values from an Array A value extracted from an array can be treated like any other value, e.g. (assuming that the relevant declarations and initializations have been done) System.out.println(marks[1]); total += marks[counter]; aStudent.calculateTotalMark(marks[count er]);

Displaying the Contents of the Array public void printArray() { int[] marks = {10, 45, 67, 88, 0, 66}; System.out.println(); int counter = 0; while (counter < marks.length){ System.out.println(marks[counter]); counter++; }

Another Way of Displaying the Array public void printArray() { int[] marks = {10, 45, 67, 88, 50}; System.out.println(); for (int counter = 0; counter < marks.length; counter++) System.out.println(marks[counter]); } marks.length returns the number of elements in the marks array. Note that this is not a method call. Do not confuse this with the String class's length() method.

Creating an Array of float s As with any object, the name of an array is a reference to the array itself. To put aside some memory, we need to use the new operator. double[] prices = new double[6]; prices

Creating an Array of Objects An array of objects is actually an array of references to objects. Person[] family = new Person[6]; family These references have not been initialized. They have NULL values. We have not yet created the Person objects themselves.

An Array of Objects For each element of the array, we need to create an object that it references. family

Creating the Objects for the Array public static void main(String [] Args) { Person[] family = new Person[6]; for (int counter = 0; counter < family.length; counter++) family[counter] = new Person(); for (int counter = 0; counter < family.length; counter++) { System.out.println("Name?"); family[counter].setName(Keyboard.readString()); } … }

Passing Arrays as Arguments  When passing an entire array to a method, the name of the array is passed without any brackets. The name of an array is a reference to the array.  Brackets in the formal parameter show it is an array, without specifying its size. The array knows how big it is.  Because a method is passed a reference to the array, a change to its contents in the called method will change it in the calling method.

Arrays as Parameters public void passArray() { String[] food = {"apple", "fig", "banana", "orange"}; System.out.println(countShortWords(food)); } private int countShortWords(String[] words) { int shortWords = 0; for (int counter = 0; counter < words.length; counter++) if (words[counter].length() < 4) shortWords++; return shortWords; }... contd.

Changing the Contents of an Array in a Called Method public void changePassedArray() { String[] food = {"apple", "fig", "banana", "orange"}; changeShortWords(food); displayStringArray(food); } private void changeShortWords(String[] words) { for (int counter = 0; counter < words.length; counter++) if (words[counter].length() < 4) words[counter] = words[counter].toUpperCase(); }