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.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Introduction to arrays Data in economy size packages.
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.
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.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
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 prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
JavaScript, Third Edition
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.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
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.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
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.
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.
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.
Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array.
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.
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.
Foundation Studies Course M.Montebello Arrays Foundation Studies Course.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Pointers *, &, array similarities, functions, sizeof.
1 Arrays 1090CS, Computer Programming 1 Manesh T
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
BIT115: Introduction to Programming
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
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.
1 2. Program Construction in Java. 01 Java basics.
CMSC 150 ARRAYS CS 150: Fri 10 Feb Motivation  Consider a list of your contact addresses: String Zero = String.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Array in C# Array in C# RIHS Arshad Khan
Array, Strings and Vectors
Arrays ICS 111: Introduction to Computer Science I
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Introduction To Programming Information Technology , 1’st Semester
MSIS 655 Advanced Business Applications Programming
C Programming Pointers
Java: Variables, Input and Arrays
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Python fundamental.
Presentation transcript:

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 class…200 results = 200 variable declarations?  Solution is to use Arrays… An array is a structure that holds multiple values of the same type. The length of an array is established when the array is created (at runtime). After creation, an array is a fixed-length structure.

 Arrays are Objects and must be declared as such (use the new key word) int arrayExample[] = new int[10];  Creates an array of 10 Integers (all elements are of the same datatype e.g. int, float, Student etc.)  Two parts to declaration… int arrayExample[] tells compiler there will be an array of integers referenced by the name arrayExample new int[10]assigns the size of the Array (sets it aside in memory – space for 10 integers needed) Arrays

Where confusion can arise… Arrays can be declared and initialised in either of the following ways… int[] arrayExample = new int[10]; int arrayExample[] = new int[10];  Both are correct and are down to preference, many different text books and programmers use both styles Declare & Create in one statement

 Each place holder in an array is known as an element (we have 10 elements of type integer)  Each element is uniquely identified by an index  To refer to an element… arrayExample[index]  NB Index values start at 0!!!!!  Each element in an array has the same name as the array but a different value (index) in the square brackets Arrays

Creating an Array  rainfall = new double[12];  This creates an array which can hold 12 elements.  The first element will be stored at –rainfall[0]  The last element will be stored at –rainfall[11]  NOTE: the index of the first position in an array is 0.

 If you try to reference an element using a higher index, this will result in an Exception (error) - ArrayIndexOutOfBoundsException  When Arrays are first created the elements of the Arrays are initialised to 0 (false for Boolean and null for objects) Arrays

Dynamic Initialisation dataType nameOfArray[] = { };  There is no need for a ‘new’ statement, it will be done automatically  Example int values[] = {4,2,7,1};

See Example 1

Arrays of Objects  Arrays can hold objects in the same way they held fundamental data types  Declaration for the array is the same: className arrayName[]; // Array creation step 1 arrayName = new className[3]; // Array step 2

Accessing objects in an array Use the array index as if it were an object variable arrayName[0].methodName();arrayName[1].variableName;

Example class Student { String name; String ID; int mark; public void setName (String n) {.....} public void setid (String Id) { } Student bis[] = new Student[3]; bis[0] = new Student(); bis[0].setName ("Paul"); bis[0].setId ("987654");

See Example 2