Introduction to arrays Data in economy size packages.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

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.
Kernighan/Ritchie: Kelley/Pohl:
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
 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.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays Chapter 6 Chapter 6.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Introduction to arrays. Array Homogeneous collection of components stored in adjacent memory locations –All elements share same data type –Entire collection.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 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.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
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.
August 6, 2009 Data Types, Variables, and Arrays.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
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.
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.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
CSE 1201 Object Oriented Programming ArrayList 1.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
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.
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.
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.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
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.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
CMSC 202 ArrayList Aug 9, 2007.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Pass by Reference, const, readonly, struct
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.
CMSC 202 ArrayList Aug 9, 2007.
Introduction To Programming Information Technology , 1’st Semester
Chapter 7 The Java Array Object © Rick Mercer.
CMSC 202 ArrayList Aug 9, 2007.
CS2011 Introduction to Programming I Arrays (I)
Arrays in Java.
C++ Array 1.
Presentation transcript:

Introduction to arrays Data in economy size packages

Array Data structure that contains a collection of data items –All items are of the same data type –Access to the entire collection via the array name –Access to individual elements via their indexes (or subscripts) Behaves like a list of variables with a uniform naming mechanism

Example Suppose you needed to store 20 distinct whole-number values; what can you do? –Declare 20 distinct int variables, giving each a unique name –Declare a single int array with the capacity to hold 20 elements Which is preferable? What if the problem involved 200 numbers? 2000? 2,000,000?

Array declaration The syntax for array declaration is: data type [ ] identifier; –“data type” is any built-in, library or user- defined data type (int, double, char, String, etc.) –“identifier” is a valid Java identifier –Java supports a variation of this syntax, which is closer to C/C++ syntax: data type identifier[ ];

Array creation An array is a reference data type, similar to a class newDeclaration doesn’t allocate any memory; this is done, as with objects, with the new operator Examples: double [] averages = new double[10]; char [] alphabet; alphabet = new char[26]

Array creation The number in square brackets when an array is created indicates the number of elements that can be stored in the array Any positive int expression can be used to allocate an array Example: public static final int LETTERS = 26; char [] asciiAlphabet = new char [LETTERS * 2];

Array indexing & length The length of the array is determined when it is created; the value is stored in a public instance variable, length asciiAlphabet.length has the value 52 If an array is created with size n, the subscripts of its elements are numbered (in order) from 0 to n-1 So, for example, the first element in the asciiAlphabet array is element 0, and the last element is element 51

One overloaded operator We have already seen two uses for the indexing operator ([]): –Array declaration; for example, int [] nums; –Array creation; nums = new int [20]; This operator has one more use: access to individual array elements, or indexing

Accessing individual array values An element is accessed via its index or subscript The notation for accessing an element via its subscript is: arrayName[subscript] where –“arrayName” is the array’s identifier –“subscript” is a number between 0 and size-1 The first element has subscript 0, the second has subscript 1, the third has subscript 2, and so forth up to the last element, which has subscript length-1

Examples asciiAlpha[0] = ‘A’; System.out.print(“” + asciiAlpha[8]); averages[7] *= 0.5; Note: although these examples use literal values, a subscript can be any int expression It is up to the programmer to ensure that the subscript value is within the bounds of the array

Filling an array with initial values char [] upperAlpha = new char [26]; for (char c = ‘A’; c <= ‘Z’; c++) upperAlpha[(int)(c – ‘A’)] = c; Trace: cc – ‘A’value stored ‘A’ 0 ‘A’ ‘B’ 1 ‘B’ ‘C’ 2 ‘C’... ‘Z’ 25 ‘Z’ ‘[‘ 26

Initializing at declaration char [] lowerAlpha = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’}; List all values in a block of code, terminating with a semicolon: Compiler automatically sizes array to hold the specified data; array will remain this size, even if different data assigned: for (int x = 0; x < 10; x++) lowerAlpha[x]=‘!’

13 What values are assigned? float [] temps = float[ 5 ] ; // allocates memory for array int m ; for (m = 0; m < 5; m++) { temps[ m ] = m  0.2 ; } temps[0] temps[1] temps[2] temps[3] temps[4] ? ? ? ? ?

14 Now what values are printed? float [] temps = float[ 5 ] ; // allocates memory for array int m ;..... for (m = 4; m >= 0; m-- ) { System.out.println(“” + temps[ m ]) ; } temps[0] temps[1] temps[2] temps[3] temps[4]

15 Variable Subscripts float [] temps = new float[ 5 ] ;//allocates memory for array int m = 3 ; What is temps[ m + 1] ? What is temps[ m ] + 1 ? temps[0] temps[1] temps[2] temps[3] temps[4]

Arrays of objects Arrays can be collections of elements of any data type, including classes For example, suppose we wanted to create an array of Fraction objects: –Array declaration: Fraction [] ratios = new Fraction[100]; but not the individual Fraction objects it will contain –This creates the array, but not the individual Fraction objects it will contain; these have to be created individually, as shown on the next slide

Initializing an array of objects for (int x = 0; x < ratios.length; x++) ratios[x] = new Fraction(); // initializes entire array with values 1/1 Random rg = new Random(); for (int y = 0; y < ratios.length; y++) ratios[y] = new Fraction (rg.nextInt(10) +1, rg.nextInt(20) + 1); // initializes array with random values

Methods & array parameters An array element can be passed to any method that takes its base type as an argument For example, in the Fraction class the plus method takes a Fraction argument; elements of the array declared on the previous slide could serve as both calling object and argument: ratios[0] = ratios[1].plus(ratios[2])

Methods & array parameters An entire array can be passed as an argument to a method, if such a parameter is specified – example: public static void fillFractionArray (Fraction [] fracs) { Random rg = new Random(); for (int x = 0 ; x < fracs.length; x++) fracs[x] = new Fraction (rg.nextInt(10)+1,rg.nextInt(20)+1); A call to this method: Fraction [] ratios = new Fraction[25]; Fraction.fillFractionArray(ratios); Note that the square brackets are not used when an entire array is passed as an argument

public static void main (String [] args) { What is args? A. A String B. An array of Strings C. Nothing D. None of the above