27-Jun-16 Arrays. 2 Multiple values An array lets you associate one name with a fixed (but possibly large) number of values Arrays are like Python’s lists,

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

BNF. What is BNF? BNF stands for “Backus-Naur Form,” after the people who invented it BNF is a metalanguage--a language used to describe another language.
Chapter 10 Introduction to Arrays
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.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
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.
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.
Numbers and Arrays. Widening and narrowing Numeric types are arranged in a continuum: double float long int short byte, char You can easily assign a narrower.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
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.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
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.
Chapter 9 Introduction to 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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
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.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
COMP More About Arrays Yi Hong June 05, 2015.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
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.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
C Programming Tutorial – Part I
Two-Dimensional Arrays
Arrays An Array is an ordered collection of variables
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 6-Dec-18.
7 Arrays.
Arrays 2-May-19.
Arrays.
String Objects & its Methods
Presentation transcript:

27-Jun-16 Arrays

2 Multiple values An array lets you associate one name with a fixed (but possibly large) number of values Arrays are like Python’s lists, but much less flexible All values must have the same type The values are distinguished by a numerical index between 0 and array size minus myArray

3 Using array elements Examples: x = myArray[1];// sets x to 43 myArray[4] = 99;// replaces 14 with 99 m = 5; y = myArray[m];// sets y to -57 z = myArray[myArray[9]];// sets z to myArray

4 Array values An array may hold any type of value All values in an array must be the same type For example, you can have: an array of integers ( int s) an array of String s an array of Person In this case, all the elements are Person s; but they may belong to different subclasses of Person For example, if you have a class Employee extends Person, then you can put Employee s in your array of Person This is because an Employee is a Person You can even have arrays of arrays, for example, an array of arrays of int

5 Declaration versus definition Arrays are objects Creating arrays is like creating other objects: the declaration provides type information and allocates space for a reference to the array (when it is created) the new definition actually allocates space for the array declaration and definition may be separate or combined Example for array objects: int[ ] myArray; // declaration This declares myArray to be an array of integers Notice that the size is not part of the type myArray = new int[10]; // definition new int[10] creates the array int[ ] myArray = new int[10]; // both

6 Two ways to declare arrays You can declare more than one variable in the same declaration: int a[ ], b, c[ ], d; // notice position of brackets a and c are int arrays b and d are just int s Another syntax: int [ ] a, b, c, d; // notice position of brackets a, b, c and d are int arrays When the brackets come before the first variable, they apply to all variables in the list But... In Java, we typically declare each variable separately

7 Array assignment Array assignment is object assignment You get another reference to the same array The following is legal: int[ ] myArray = new int[10];...and later in the program, myArray = new int[500]; This is legal because the array’s size is not part of its type, but part of its value

8 Array assignment When you assign an array value to an array variable, the types must be compatible The following is not legal: double[ ] dub = new int[10]; // illegal The following is legal: int[ ] myArray = new int[10];...and later in the program, myArray = new int[500]; // legal! This is legal because the array’s size is not part of its type, but part of its value

9 Length of an array Arrays are objects Every array has an instance constant, length, that tells how large the array is Example: for (int i = 0; i < scores.length; i++) System.out.println(scores[i]); Use of length is always preferred over using a constant such as 10 Arrays have a length variable, Strings have a length() method

10 Stepping through an array The for loop is ideal for visiting every value in an array The form is: for (int i = 0; i < myArray.length; i++) {...} Example: for (int i = 0; i < students.length; i++) { System.out.println(students[i].name); } In general we like to use meaningful names for variables, but in this case, the name i is traditional, and better i is instantly recognizable as the index of an enclosing for loop Inner (nested) loops should use j, then k (then, if necessary, m, then n, but not l – do you see why?) You should avoid deeply nested loops—three is deep enough! It’s usually best to declare i right in the for statement itself i++ means “add 1 to i ”

11 Array names The names of array variables should be camelCase Use lowercase for the first word and capitalize only the first letter of each subsequent word that appears in a variable name Array names should (usually) be plural nouns Example array names: scores phoneNumbers preferredCustomers

12 Arrays of objects Suppose you declare and define an array of objects: Person[ ] people = new Person[20]; You have given a value to the array named people, but you haven’t yet given values to each element in that array There is nothing wrong with this array, but it has 20 references to Persons in it all of these references are initially null you have not yet defined 20 Persons For example, people[12].name will give you a nullPointerException

13 Initializing arrays I Here’s one way to initialize an array of objects Person[ ] people = new Person[20]; for (int i = 0; i < people.length; i++) { people[i] = new Person("Dave"); } This approach has a slight drawback: all the array elements have similar values

14 Initializing arrays II There is a special syntax for giving initial values to the elements of arrays This syntax can be used in place of new type [ size ] It can only be used in an array declaration The syntax is: { value, value,..., value } Examples: int[ ] primes = { 2, 3, 5, 7, 11, 13, 19 }; String[ ] languages = { "Java", "C", "C++" };

15 Array literals You can create an array literal with the following syntax: type [ ] { value1, value2,..., valueN } Examples: myPrintArray(new int[] {2, 3, 5, 7, 11}); int[ ] foo; foo = new int[]{42, 83};

16 Initializing arrays III To initialize an array of Person : Person[ ] people = { new Person("Alice"), new Person("Bob"), new Person("Carla"), new Person("Don") }; Notice that you do not say the size of the array The computer is better at counting than you are!

17 Arrays of arrays The elements of an array can themselves be arrays Once again, there is a special syntax Declaration: int[ ][ ] table; (or int table[ ][ ]; ) Definition: table = new int[10][15]; Combined: int[ ][ ] table = new int[10][15]; The first index ( 10 ) is usually called the row index; the second index ( 15 ) is the column index An array like this is called a two-dimensional array

18 Example array of arrays int[ ][ ] table = new int[3][2]; or, int[ ][ ] table = { {1, 2}, {3, 6}, {7, 8} }; For example, table[1][1] contains 6 table[2][1] contains 8, and table[1][2] is “array out of bounds” To “zero out this table”: for (int i = 0; i < 3; i++) for (int j = 0; j < 2; j++) table[i][j] = 0; How could this code be improved?

19 Size of 2D arrays int[ ][ ] table = new int[3][2]; The length of this array is the number of rows: table.length is 3 Each row contains an array To get the number of columns, pick a row and ask for its length: table[0].length is 2  Most of the time, you can assume all the rows are the same length  However: table[2] = new int[50];

20 The End “All programmers are optimists. Perhaps this modern sorcery especially attracts those who believe in happy endings and fairy godmothers. Perhaps the hundreds of nitty frustrations drive away all but those who habitually focus on the end goal. “Perhaps it is merely that computers are young, programmers are younger, and the young are always optimists. But however the selection process works, the result is indisputable: ‘This time it will surely run’ or ‘I just found the last bug’.” --Fred Brooks