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
Chapter 8: Arrays.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
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 Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
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  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
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
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
1 One-Dimensional Arrays  What are and Why 1-D arrays?  1-D Array Declaration  Accessing elements of a 1-D Array  Initializer List  Passing Array.
Saravanan.G.
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.
 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.
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.
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
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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, 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
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
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.
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.
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.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
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,
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays in Java.
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 on p. 479 import java.io.*; public class ReversePrintII { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); public static void main (String[] args) throws IOException { int[] item = new int[5]; int sum, int counter; System.out.println("Enter five integers, one " + "integer per line"); sum = 0; for(counter = 0; counter < 5; counter++) { item[counter] = Integer.parseInt(keyboard.readLine()); sum = sum + item[counter]; } System.out.println("The sum of the numbers = " + sum); System.out.print("The numbers in the reverse order are: "); for(counter = 4; counter >= 0; counter--) System.out.print(item[counter] + " "); System.out.println(); }

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];

8 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;

10 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];