Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CiS 260: App Dev I

2 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 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};

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


Download ppt "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."

Similar presentations


Ads by Google