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.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 8: Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Arrays Chapter 6 Chapter 6.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 8 Arrays and Strings
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
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.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
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.
Java Programming: Chapter 9: Arrays
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
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.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Array 1 ARRAY. array 2 Learn about arrays. Explore how to declare and manipulate data into arrays. Understand the meaning of “array index out of bounds.”
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Computer Programming BCT 1113
Chapter 8 Multi-Dimensional Arrays
Presentation transcript:

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 a one-dimensional array, the collection is a simple ______. n To declare a one-dimensional array  int[] anArray; // reference variable declared  int anArray []; // does the same thing n To then __________ this one-dimensional array with 10 elements  anArray = new int[ 10 ]; // elements declared n You can do both in one statement  int[] anArray = new int[ 10 ]; or  int j = 10; int[] anArray = new int[ j ];

3 Accessing Array Components An array component has the form anArray[ i ] where i ranges from __ to anArray.length - 1. [] is the array subscripting __________. anArray[ 0 ] is the first element in the array and can be treated like any other __________. anArray[ 0 ] = 5; assigns 5 to anArray[ 0 ]. n The following statements also assign a value to an array element  int i = 7;  anArray[ i ] = 42; n The following declares, instantiates, and loads  double [] myArray = { 5.0, 10.0, 15.0, 20.0 };

Example import java.util.Scanner; public class ReversePrintII { public static void main (String[] args ) { int[] item = new int[ 5 ]; int sum = 0, counter; Scanner input = new Scanner( System.in ); System.out.println( "Enter five integers, "one integer per line" ); for( counter = 0; counter < 5; counter++ ) { item[ counter ] = input.nextInt(); sum = sum + item[ counter ]; } // end for System.out.println( "The sum of the numbers = " + sum ); System.out.print( "The numbers in the reverse order: “ ); for( counter = 4; counter >= 0; counter-- ) System.out.print( item[counter] + " “ ); System.out.println(); } // end main } // end class

5 Arrays as Parameters to Methods n An array is an _______ and can be passed as an argument to a method. n Example public static void main( String [] args ) { double [] myArray = { 5.0, 10.0, 15.0, 20.0 }; printArray( myArray ); } public static void printArray( double[] anArray ) { for( int i = 0; i < anArray.length; i++ ) System.out.println( anArray[ i ] ); } The _____ address of an array is the address of the first array component, such as myArray[0].

6 Parallel Arrays n Two or more arrays are parallel if their corresponding components (__________) hold related information. n Example  int [] studentID = new int[ 50 ];  double [] totalScore = new double[ 50 ];  char [] courseGrade = new char[ 50 ]; Thus, studentID[ 22 ], totalScore[ 22 ], and courseGrade[___] would all refer to the same student.

7 Arrays of Objects Arrays can hold __________ such as int ’s, double ’s and char ’s. n In Java, arrays can also hold _______ which in turn can hold data of differing types (such as primitives or other objects). n Suppose you have employees and each employee has an arrival and a departure time. n You can create arrays for employees, their arrival times, and their departure times:  Employee[] companyEmp = new Employee[ 100 ];  Clock[] arrivalTimeEmp = new Clock[ 100 ];  Clock[] departureTimeEmp = new Clock[ 100 ];

Two-Dimensional Arrays n A one-dimensional array is used for data in list form (e.g., a list of names or a list of scores). n A two-dimensional array is used for data in ____ form where all data have the same _____. n Characteristics of tables are rows and _______. n The general syntax is  dataType[ ][ ] arrayName = new dataType[ rows] [ cols ]; n Example of sales by month (0-11) for five car makers (Chrysler, GM, Ford, Honda, Toyota):  double[][] sales = new double[ 12 ][ 5 ];  sales[ 5 ][ 3 ] = 25.75;//store in row 5, col 3  sales[ 7 ] is the sales array for all cars in _______

9 Ragged Two-Dimensional Arrays n A ragged two-dim array has uneven _______. int [][] raggedArray = new int [ 3 ]; // # of rows raggedArray[0] = new int[ 4 ]; // # cols in 1st row raggedArray[1] = new int[ 1 ]; raggedArray[2] = new int[ 2 ]; raggedArray[0][0] = 6; raggedArray[0][1] = 17; … n Load a two-dim array at declaration  int [][] raggedArray = { {6,17,5,2},{14},{-2,8} }; n Load a two-dim array using ________ loops for( int i = 0; i < raggedArray.length; i++ ) for( int j = 0; j < raggedArray[j].length; j++ ) raggedArray[ i ][ j ] = i;

Multidimensional Arrays n Multi-dim arrays just extend two-dim arrays. n Load random numbers in a 3-dim array double [][][] randomNums = new double [5][5][5]; for(int i=0; i<randomNums.length; i++) for(int j=0; j<randomNums[i][j].length; j++) for(int k=0; k<randomNums[i][j][k].length; k++) randomNums[i][j][k] = Math.random(); n Add the random numbers in the array double sum = 0; for(int i = 0; i < randomNums.length; i++) for(int j = 0; j < randomNums[i][j].length; j++) for(int k = 0; k < randomNums[i][j][k].length; k++) sum += randomNums[i][j][k];

11 Predefined Methods for Arrays The class Arrays contains several useful methods: n Arrays.toString( arrayA) // converts to Strings n arrayA.equals( arrayB ) // tests “total” equality n Arrays.sort( arrayA ) // sort arrayA ascending n Arrays.search( arrayA, value ) // search arrayA n Arrays.fill( arrayA, 0 ) // fill arrayA w/ 0’s