Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using.

Slides:



Advertisements
Similar presentations
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
 9: Arrays  Why?  What is an Array?  Declaring and Creating an Array  Initializing and Array  Array Operations  Array Examples  Arrays of Objects.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
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.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
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. 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.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01. OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing.
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.
Lecture 3: Strings, Screen Output, and Debugging Yoni Fridman 7/2/01 7/2/01.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
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.
 Pearson Education, Inc. All rights reserved Arrays.
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.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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 Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
PHY281 Scientific Java Programming ArraysSlide 1 Arrays In this section we will learn how about storing groups of related variables that share the same.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
BIT115: Introduction to Programming
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
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];
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
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.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Lecture 8: 2D Arrays and Nested Loops
Chapter 7 Part 1 Edited by JJ Shepherd
A simple way to organize data
Arrays An Array is an ordered collection of variables
Programming Control Structures with JavaScript Part 2
Presentation transcript:

Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01

OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using arrays ä Examples ä Accessing arrays sequentially ä Accessing arrays arbitrarily ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using arrays ä Examples ä Accessing arrays sequentially ä Accessing arrays arbitrarily

What Are Arrays? ä All of the variables we’ve used so far store a single value each. ä What if we want to store a bunch of related values? ä Example: A deck of cards. ä An array is a structure that stores multiple values, all of which must be of the same type. ä Each individual value is called an element of the array. ä All of the variables we’ve used so far store a single value each. ä What if we want to store a bunch of related values? ä Example: A deck of cards. ä An array is a structure that stores multiple values, all of which must be of the same type. ä Each individual value is called an element of the array

Creating Arrays ä Declaring an array is the same as declaring any normal variable, except you need to add square brackets [].  For example, we would declare an array of ints like this: int[] deckOfCards;  Now deckOfCards can hold any number of ints, not just one. ä Similarly, we can declare arrays of other types:  String[] classRoll; declares an array of Strings. ä Problem: How many elements can the array hold? ä Declaring an array is the same as declaring any normal variable, except you need to add square brackets [].  For example, we would declare an array of ints like this: int[] deckOfCards;  Now deckOfCards can hold any number of ints, not just one. ä Similarly, we can declare arrays of other types:  String[] classRoll; declares an array of Strings. ä Problem: How many elements can the array hold?

Creating Arrays ä Arrays are initially of length zero. ä Before we use an array, we must allocate space for it (tell it how many elements we want it to have).  When you allocate space with the new keyword, all elements are given default values (0 for ints ). ä What happens if you try to use an array before allocating space for it? ä Arrays are initially of length zero. ä Before we use an array, we must allocate space for it (tell it how many elements we want it to have).  When you allocate space with the new keyword, all elements are given default values (0 for ints ). ä What happens if you try to use an array before allocating space for it? int[] deckOfCards; deckOfCards = new int[52]; OR int[] deckOfCards = new int[52]; int[] deckOfCards; deckOfCards = new int[52]; OR int[] deckOfCards = new int[52];

Using Arrays  Like we saw with Strings, each element of an array has an index, starting from zero: Notice that an array with n elements has indices 0 through n-1. ä So how do we use arrays?  To access the third array element, for example, we write deckOfCards[2].  This now acts as a normal int variable.  Like we saw with Strings, each element of an array has an index, starting from zero: Notice that an array with n elements has indices 0 through n-1. ä So how do we use arrays?  To access the third array element, for example, we write deckOfCards[2].  This now acts as a normal int variable

Using Arrays ä What willthis example do? ä Warning:Make sure you never try to access the n th element of an array that has n elements. ä For example, deckOfCards[52] will give an out-of-bounds error. (Remember, the 52 nd element is deckOfCards[51].) ä This is our first example of a run-time error.  Note: To find the length of deckOfCards, for example, write deckOfCards.length. ä What willthis example do? ä Warning:Make sure you never try to access the n th element of an array that has n elements. ä For example, deckOfCards[52] will give an out-of-bounds error. (Remember, the 52 nd element is deckOfCards[51].) ä This is our first example of a run-time error.  Note: To find the length of deckOfCards, for example, write deckOfCards.length. deckOfCards[3] = 7; deckOfCards[1] = deckOfCards[3]; System.out.println(deckOfCards[1]); deckOfCards[3] = 7; deckOfCards[1] = deckOfCards[3]; System.out.println(deckOfCards[1]);

Accessing Arrays Sequentially ä In many cases, you want to look at each array element, one at a time.  A for loop is perfect for this. ä Code example: Summing the elements of an array. ä Code example: Finding the largest element in an array. ä In many cases, you want to look at each array element, one at a time.  A for loop is perfect for this. ä Code example: Summing the elements of an array. ä Code example: Finding the largest element in an array.

Accessing Arrays Arbitrarily ä Sometimes you want to access some elements of an array, but not others. Furthermore, you might not want to go in order. ä Example: Counting cards. ä Sometimes you want to access some elements of an array, but not others. Furthermore, you might not want to go in order. ä Example: Counting cards.

HomeworkHomework ä Read: 13.1 (to end of p. 545)