Arrays ICS2O.

Slides:



Advertisements
Similar presentations
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Advertisements

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.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
Chapter 8: Arrays.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Pointers *, &, array similarities, functions, sizeof.
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.
PHY281 Scientific Java Programming ArraysSlide 1 Arrays In this section we will learn how about storing groups of related variables that share the same.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
 2005 Pearson Education, Inc. All rights reserved Arrays.
Windows Programming Lecture 03. Pointers and Arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Array in C# Array in C# RIHS Arshad Khan
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Two-Dimensional Arrays
Arrays Low level collections.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Pamela Moore & Zenia Bahorski
Complex data types Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings,
Foundations of Programming: Arrays
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 8 Arrays Objectives
Arrays in C.
Arrays, For loop While loop Do while loop
Variables ICS2O.
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 8 Arrays Objectives
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.
Java Programming Arrays
Introduction To Programming Information Technology , 1’st Semester
Arrays .
Topics discussed in this section:
MSIS 655 Advanced Business Applications Programming
Arrays.
Announcements Lab 6 was due today Lab 7 assigned this Friday
Building Java Programs
Arrays ICS2O.
CS150 Introduction to Computer Science 1
Chapter 8 Arrays Objectives
CS150 Introduction to Computer Science 1
C Programming Pointers
CS1001 Lecture 14.
Variables and Computer Memory
CS150 Introduction to Computer Science 1
Arrays.
How do you do the following?
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

Arrays ICS2O

Objectives Learn about arrays and when to use them Learn the syntax for declaring/defining a new array of any data type

What is an Array? An array of doubles An array is a collection of related data of the same type. It is stored as a block of consecutive memory locations for quick access by the computer. Individual locations are called the array’s elements. When we say “element” we often mean the value stored in that element. 1.39 1.69 1.74 0.0 An array of doubles

What is an Array (cont’d) Rather than treating each element as a separate named variable, the whole array gets one name. Individual array elements are referred to by using the array’s name and the element’s numbered address, called the index. For example, we could say the value 1.74 is stored in the nums array at index 2 nums[0] nums[1] nums[2] nums[3] 1.39 1.69 1.74 0.0 nums is this array’s name

Indices In Java, an index is written within square brackets following the array’s name, e.g. Assuming the array is named nums, nums[0] will reference the value in the nums array at index 0. Indices start from 0; the first element of an array nums is referred to as nums[0] and the n-th element as nums[n-1]. n-1 because of the 0 start. An index must be any whole number value from 0 to the array’s length-1 (total number of elements - 1)

Indices (cont’d) An index can be an integer variable or any expression that evaluates to an integer value. For example: (Assuming the array is named nums) nums[3]  references the 4th element of the array nums[studentNumber] nums[studentNumber – 2]

Indices (cont’d) In Java we MUST define an array with a specific length, think of it like reserving a table at a restaurant with a precise number of seats. This implies you cannot put more people at the table than you have chairs. The same works for an array, we cannot store more data than we promised to store. In Java, while the program is running if you try to access an element not in the range of indices (from 0 to length – 1) an “out of bounds” error will occur and crash the program.

Why Do We Need Arrays? The power of arrays stems from data organization. If we did not use arrays we would be forced to use individual variables in their place. Imagine creating a program that stored the name of every student in the school. You would need 1400 string variables or 1 string array with 1400 elements

Declaring(Defining) an Array Like any variable, before we can use it we must define it so it is in the Java dictionary and the memory is reserved. RECALL: Defining an int variable named num int num; Defining an array is the same, but we have to remember we are defining a collection of data, so our names are typically pluralized. We also must inform the compiler that we are defining an array and how big it will be Example: Define an array of 5 ints named nums int [] nums = new int[5]; The square brackets tell the compiler it is an array, the second have reserves enough memory for 5 int values