What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
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.
 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 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.
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.
©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.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
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.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
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.
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.
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld.
Computer Science I Arrays Professor: Evan Korth New York University.
 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.
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.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
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.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
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.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
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.
Chapter 8: Arrays.
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 BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
BIT115: Introduction to Programming
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.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
 2005 Pearson Education, Inc. All rights reserved Arrays.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays, For loop While loop Do while loop
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.
EKT150 : Computer Programming
Object Oriented Programming in java
BIT115: Introduction to Programming
Arrays ICS2O.
MSIS 655 Advanced Business Applications Programming
Arrays Week 2.
Arrays in Java.
Presentation transcript:

What is an Array?

An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for the last five days, or stock prices for the last 30 days.) –same data type (for example, you can create an array of ints or an array of doubles, but you cannot mix and match ints with doubles.) –The size of an array is fixed once it is created.

What exactly are we talking about?! Let’s say you have ten students and you want to save the grades for each of them to use in your program. Could do it the hard way: Set up ten variables called studentOneGrade, studentTwoGrade, studentThreeGrade, etc. Instead, use an array to do it the easy way…

Using an array variable Create an array variable called studentGrades[10] Declared it: int [] studentGrades = new int[10]; This sets up a location in memory for 10 integers which can be accessed using: studentGrades[ # ] # is the student you want to look at.

Parts of an Array

Parts of the array The array has some terms we haven’t seen yet: –Elements –Index (or Position Number) –Subscript –Zeroth element –Keyword “new”

Elements Items in the array. For example, –an array of 10 integers has 10 elements –an array of 15 floats has 15 elements –an array of 5 characters has 5 elements –and so on…

Index (Position Number or Subscript) Refers to one particular element in the array The first element in an array uses an index of 0 (zero). For example, –studentGrades[ 0 ] The second position is referred to by –studentGrades[ 1 ]

Figuring out the array positions In Java, an arrays’ elements start out at index 0 and go up to (the number of elements – 1) For example, our array of 10 student grades filled in with grades might look like: Value Index

Array positions (cont’d) We can access them by using the array name and square brackets with the index number between them. For example, System.out.println ("The third student’s grade is " + studentGrades[ 2 ] ); Would print only the third integer spot in the array (remember 0 is the first, 1 is the second, and 2 is the third element’s index) The output would look like the following: The third student’s grade is 99

Array positions (cont’d) Remember, arrays start at “0”, not one! The element in the first position is sometimes called the zeroth element. Notice the difference between "position" and "element number"

12 Keyword new The keyword new is used in Java when you wish to create a new object. In Java, arrays are objects. All positions of the new array will be initialized to the default value for the type. –Numeric types: 0 –char \nul (0) –boolean false

Using Arrays

So how do we use arrays? Same concepts as other variables apply –Must declare the array –Must initialize the array (done for you) Array variables are actually reference variables. –Can use arrays in expressions and methods, just like other variables

Declaring an array First you can declare an array reference variable (you must specify the type of the elements in the array) : –int [] myFirstArray;//declares an array //variable for ints –Note: the line above does not allocate memory for the array (it cannot since we have not said the size of the array yet). It only sets aside enough memory to reference the array. Until we create an array, the reference is to null. Next, we create the array and “point” the reference to it: –myFirstArray = new int [10]; –To create the array, we need the number of elements in the array so the computer can set aside memory for the array. We can combine the declaration and allocation lines into one line as follows: –int [] myFirstArray = new int [10];

16 Declaring arrays (cont) You can use a constant to set the size of the array final int GRID_ROWS_MAX 8; int [] gridRows = new int [ GRID_ROWS_MAX ];

Initializing an Array You can initialize an array when you declare it, as you do with other variables Syntax is slightly different, as you are now initializing more than one element at a time One way at declaration, using initializers: int myFirstArray[ ] = { 1, 1, 1, 1, 1 }; Note the braces around the initial values. The values themselves are separated by commas. Also note that creating arrays in this way eliminates the need for the keyword new.

Initializing array with a for loop After declaring an array, you can initialize it in the body of your program by using a for loop: int [] myFirstArray = new int [ 5 ]; for (int i = 0 ; i <= 4 ; i++ ) { myFirstArray[ i ] = 1 ; } // end for Note the top value is 4, not 5! That is, you loop through 0 to 4 to initialize an array with 5 elements

Some powerful features of arrays Can set array elements equal to other values or expressions. For example, studentGrades[ 1 ] = 100 ; This would set the array element with a subscript of 1 to a grade of 100. That is, the second student would have a grade of 100. Java allows us to access an array’s length by using the following notation: –studentGrades.length would evaluate to 10

powerful features (cont) Can use expressions as the subscript –E.g. if variables a = 1 and b = 2 studentGrades[ a + b ] would be the same as writing studentGrades[ 3 ] Can use array elements in expressions –E.g. int gradeTotal = 0 ; gradeTotal = studentGrades[ 0 ] + studentGrades[ 1 ] + studentGrades[ 2 ] + …etc… studentGrades[ 9 ] ; –Would add up all the array elements and store them in gradeTotal.

Accessing elements with for loop Can use a for loop to print the contents of an array int [] myFirstArray = new int [ 5 ]; for (int i = 0 ; i <= myFirstArray.length – 1; i++ ) { System.out.println ("array element " + i + " is equal to " + myFirstArray [i]); } // end for Note the use of myFirstArray.length instead of using the size itself. It is better style to do so. Why?

Array Bounds Very Important: Java does not provide array bounds checking at compile time. This means that your program will crash at run time of you try to access memory outside of an array’s bounds. For example, suppose you have: int [] myFirstArray = new int [ 10 ]; myFirstArray [100] = 45; 100 is very far outside your defined size (0..9) However, the compiler will not indicate an error.