Arrays Introduction to Arrays Reading for this Lecture:

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision Making Selection structures (if....else and switch/case)
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
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.
Week 3 Part 2 Kyle Dewey.
GC211 Data Structure Lecture 1 Sara Alhajjam.
Arrays Low level collections.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Compound Condition Break , Continue Switch Statements in Java
Unit-1 Introduction to Java
Decisions Chapter 4.
Foundations of Programming: Arrays
JavaScript: Functions.
Counted Loops.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, For loop While loop Do while loop
Introduction to Programming
Java - Data Types, Variables, and Arrays
Selection (if-then-else)
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.
Lecture 18 Arrays and Pointer Arithmetic
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
The switch statement: an alternative to writing a lot of conditionals
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Logical Operations In Matlab.
CS2011 Introduction to Programming I Arrays (I)
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Objectives You should be able to describe: Addresses and Pointers
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CISC181 Introduction to Computer Science Dr
Arrays ICS2O.
1D Arrays and Lots of Brackets
CS150 Introduction to Computer Science 1
INC 161 , CPE 100 Computer Programming
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
CS150 Introduction to Computer Science 1
Program Flow.
Fundamental Programming
CS150 Introduction to Computer Science 1
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Arrays Introduction to Arrays Reading for this Lecture: Section 7.1-7.2

Introduction to Arrays It is very useful to have a group of variables that can be processed in a loop where one variable is processed during each pass through the loop But we don’t want to declare them as individual variables, e.g. five individual integer variables: int book0; book1, book2, book3, book4; We can’t use a loop index variable to refer to one variable book0, book1, etc without a lot of nested if-else statements or a switch statement

Introduction to Arrays Without arrays we would need to do something like this (NOTE: Don’t do it this way!): int book0, book1, book2, book3, book4; for (int i = 0; i < 5; i++) { switch (i) { case 0: statements using book0; break; case 1: same statements using book1; // three more cases needed here }

Introduction to Arrays We can declare an array of variables of a specific type with the capability to use an index variable to select one variable int [ ] nums = new int [5]; The above declares 5 variables of type int A single int variable can be selected using an integer expression or value inside the [ ]: nums[integer expression or value] The valid array index values are 0-4 (not 1-5)

Arrays and Initializer Lists An array can be defined and initialized so that each element contains a specific value: char [] vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; Java uses the initializer list to determine how long the array must be and allocates that many elements An initializer list can be used only when the array is first declared, as above Afterward, each element of the array can only be initialized individually, for example: vowels[3] = ‘o’;

Arrays and Loops Now we can coordinate the processing of one variable with the execution of one pass through a loop using an index variable, e.g: int MAX = 5; // symbolic constant int [ ] nums = new int [MAX]; for (int i = 0; i < MAX; i++) { // use i as array index variable Java statements using nums[i]; }

Alternative Loop Control Condition Arrays are objects Each array has an attribute “length” that we can access to get a value equal to the length of that array, e.g.nums.length is equal to MAX: int MAX = 5; // symbolic constant int [ ] nums = new int [MAX]; for (i = 0; i < nums.length; i++) { // use i as array index variable in Java statements using nums[i]; }

Alternative Loop Control Condition Using an array’s length attribute allows us to loop through the entire array when we have no prior knowledge of the length of the array See L&L, Page 375 ReverseOrder.java