Chapter 10 Arrays. Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Chapter 8: Arrays.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
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.
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Arrays Chapter 6 Chapter 6.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Java Unit 9: Arrays Declaring and Processing Arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Chapter 9 Control Structures.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Data Structures Arrays and Lists Part 2 More List Operations.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
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.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Object Oriented Programming Lecture 2: BallWorld.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter VII: Arrays.
Arrays Chapter 7.
Chapter 10 Arrays.
Blitzen’s Boogie Teresa and Paul Jennings Music K-8, Vol. 12. Num.2 © 2001 Plank Road Publishing, Inc. All Rights Reserved- used by permission PowerPoint.
Get Along, Little Reindeer
C++ Arrays.
Chapter 10 Arrays.
Arrays An Array is an ordered collection of variables
Chapter 6 Arrays.
PowerPoint by Susan Will
An Introduction to Java – Part I, language basics
Reindeer Names.
Please make sure that your child reads to you nightly.
CS2011 Introduction to Programming I Arrays (I)
Arrays Chapter 7.
Chapter 9: Pointers and String
Presentation transcript:

Chapter 10 Arrays

Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array. Calculate the sum, largest number, smallest number, and average of a group of numbers by using an array. Setup a program to store the arguments from command line into an array. 2

Learning Java through Alice © Daly and Wrigley Declaring an Array Array - group of contiguous memory locations that all have the same name and the same type. Declaring an array: int [ ] temp; // an integer array called temps boolean[ ] hostilePeople; // an array of hostilePeople String difficultWord [ ]; // an array of difficultWords double examScore[ ]; // an array of examScores 3

Learning Java through Alice © Daly and Wrigley Creating Array Objects int [ ] temp = new int [ 250 ] ; String [ ] reindeerName = { "Dasher", "Dancer", "Prancer", "Vixen", "Comet", "Cupid", "Donner", "Blitzen" } ; 4

Learning Java through Alice © Daly and Wrigley Length of Array String n [ ] = { "Mary", "John", "Susan", "Robert" }; int x = n.length; // where n is the name of the array n[ 0 ] = "Mary" n[ 1 ] = "John" n[ 2 ] = "Susan" n[ 3 ] = "Robert" The length of the array called n is 4. n[2] is "Susan" instead of "John" as you might expect. 5

Learning Java through Alice © Daly and Wrigley Example Array Program public static void main (String args[ ] ){ int number =5; int myArray [ ] = new int[10]; // declares integer array of 10 elements for (int i = 0; i <10; i ++) { myArray[i] = number; // puts 5, 10, 15 into elements of myArray System.out.println("The " + i + " element of the array is " + myArray [ i ] ); number += 5; } The 0 element of the array is 5 The 1 element of the array is 10 The 2 element of the array is 15 The 3 element of the array is 20 The 4 element of the array is 25 The 5 element of the array is 30 The 6 element of the array is 35 The 7 element of the array is 40 The 8 element of the array is 45 The 9 element of the array is 50 6

Learning Java through Alice © Daly and Wrigley Another Way of Writing the Previous Program public static void main (String args[ ] ) { int myArray [ ] = new int[10]; // declares integer array of 10 elements for (int i = 0, number=5; i<10; i ++, number+=5) { myArray[i] = number; // puts 5, 10, 15 into elements of myArray System.out.println("The " + i + " element of the array is " + myArray[i] ); } } 7

Learning Java through Alice © Daly and Wrigley Summing Numbers sum = sum + a; sum= sum + b; sum = sum + c; sum = sum + d; sum = sum + e; OR sum = sum + a + b + c + d + e; OR If the values were in an int array called x, we could do the same thing as follows: for ( i = 0; i < 5 ; i++) { sum = sum + x [ i ]; } 8

Learning Java through Alice © Daly and Wrigley Drawing Polygons 9