Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.

Slides:



Advertisements
Similar presentations
Arrays.
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.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Programming with Collections Collections in Java Using Arrays Week 9.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Java Syntax Primitive data types Operators Control statements.
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.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Chapter 9 Introduction to Arrays
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
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.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
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.
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.
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.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
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 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 BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
ECE122 Feb. 22, Any question on Vehicle sample code?
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Lec 23 Memory Maps Primitive variables and Reference (object) variables.
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.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Lec 21 More Fun with Arrays: For Loops. Agenda Some backfill for Lab 20: – Using an array in an applet or class – array instance variables – using Math.random()
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
1 Intro to Computer Science Arrays Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault University of.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
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];
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
ARRAYS Multidimensional realities Image courtesy of
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Array in C# Array in C# RIHS Arshad Khan
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.
Arrays .
Single-Dimensional Arrays chapter6
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Arrays in Java.
Lec 17 Using Nested Loops and Objects in an Applet Class
String Objects & its Methods
Presentation transcript:

Lec 19 Array Intro

Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells

Arrays (section 7.1 in book) – An array is a list of variables The variables within are called the elements of the array Use the elements of the array as you would any other variable (same rules for primitive vs reference, assignment, and usage) – See exampleexample – Arrays are objects, but its elements may be primitives or objects (depending on the array)

Declaring Arrays //declares an array variable called "a" which can hold a //reference to an array of boolean values boolean[] a; //declares a array variables "d" which can hold a //reference to an array of double values double[] d; //declares an array variable called "firework" which can hold a //reference to an array of Particle objects Particle[] firework;

Constructing Arrays //creates a new array with 5 boolean values and sets "a" to a //reference to that array. All the values default to false. a = new boolean[5]; //creates a new array with 10 double values and sets "d" to a //reference to that array. All the values default to 0.0 d = new double[10]; //Creates a new array with 100 Paritcle values, all of which //default to null. Then sets firework with a reference to //that array. //Note that this statement DOESN'T create any Particle objects! firework = new Particle[100];

Combined declaration and construction double[] testScores = new double[25];

Storing and Retrieving array data //note that we index starting from zero testScores[0] = 68.2; //this would be the last element of the array; since there are //25 elements it goes from 0 through 24. testScores[24] = 72.4; System.out.println(testScores[0]); //prints 68.2 System.out.println(testScores[24]); //prints 72.4 System.out.println(testScores[23]); //prints 0.0 //System.out.println(testScores[25]); //BAD! Runtime error!

Main things to know with arrays (section 7.1 in book) – Use the datatype[] varName syntax to declare a variable capable of holding an array For example: int[] myArray; – Use the new datatype[length] syntax to construct a new array object For example: myArray = new int[5]; The length of an array is fixed when it is created – The elements of the array are initialized to their usual defaults (0 if using number primitives, false if using boolean, null if using objects)

Main things to know with arrays (continued) – Refer to elements of the array using arrayName[index] For example: System.out.println(myArray[3]); Use and assignment of array elements work just like the usual variables of that type – The index to an array starts counting from zero So an array of length 3 has elements at indices 0, 1, and 2 – You can access the length of an array by using the public length instance variable For example: System.out.println(myArray.length);

A real-life example – See example of an instantiable class that uses an array of Particle objectsexampleParticle

Memory diagrams of arrays (weakly covered in section 7.1 in book) – Work out an example and see output and memory diagramexampleoutputmemory diagram – Arrays are objects and therefore variables that hold arrays are reference variables and work via references (arrows) – The elements of an array might be primitives or objects depending on what type of array it is. In either case, the usual rules apply (references for objects, actual values for primitives)

Lab19 Using Arrays You'll be creating an applet that uses arrays to store circle radii, the points of a triangle, and a list of lines from a poem: