Download presentation
Presentation is loading. Please wait.
Published byWinfred Francis Modified over 8 years ago
1
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1
2
Winter 2004CS-1010 Dr. Mark L. Hornick 2 Modern programming languages support the concept of collections That is, mechanisms to sort and manipulate many instances of the same type of data In Java, an array is an indexed collection of data values of the same type Arrays of both primitives or objects are possible Integer arrays String arrays WinPlotter arrays
3
Winter 2004CS-1010 Dr. Mark L. Hornick 3 Arrays can be used to help address the following types of problems You have data for daily rainfall collected over the past month – what is total rainfall for the month? You have data for maximum daily temperature collected over the past year – what is the average daily temperature? You have data for the names of everyone registered for classes at MSOE – sort the data alphabetically
4
Winter 2004CS-1010 Dr. Mark L. Hornick 4 Square brackets [ ] are used to declare an array double [] rainfall; or double rainfall []; An array named rainfall of type double
5
Winter 2004CS-1010 Dr. Mark L. Hornick 5 The new operator is used to allocate the memory to store the values in an array double [] rainfall; rainfall = new double [12]; // declares and then creates an array big enough to hold 12 values
6
Winter 2004CS-1010 Dr. Mark L. Hornick 6 We use a single identifier to refer to the whole collection in the array rainfall[0] = 24.5; // rainfall for 1 st day We use an indexed expression to refer to the individual elements of the collection. Arrays use zero-based indexing An individual value in an array is called an array element.
7
Winter 2004CS-1010 Dr. Mark L. Hornick 7 In Java, an array is a reference data type (like an object)
8
Winter 2004CS-1010 Dr. Mark L. Hornick 8 An array of 12 double values after all 12 are assigned values.
9
Winter 2004CS-1010 Dr. Mark L. Hornick 9 Determining array length after creation An array has a public constant length property that indicates the size of the array. for (i=0; i<rainfall.length; i++){... } This allows you to check the size of an array without knowing how it was initially declared The size of the array remains the same whether you assigned values to each element or not
10
Winter 2004CS-1010 Dr. Mark L. Hornick 10 Don’t confuse the length property of an array and the length() method of a String length() is a method for a String object, so we use the syntax for calling a method: String str = “This is a string”; int size = str.length();
11
Winter 2004CS-1010 Dr. Mark L. Hornick 11 Shortcut: An array can be initialized at the time it is declared double[] rainfall = { 1.3, 2.5, 4.2, 6.5, 0.8, 1.7, 1.5, 3.2, 6.7, 1.9, 0.3, 1.5 }; When initialized in this way, it is unnecessary to use the new operator or to state the size of the array. The length is determined by the number of values in the list
12
Winter 2004CS-1010 Dr. Mark L. Hornick 12 Using constants to declare the array sizes does not always lead to efficient use of space rainfall = new double [12]; Declaration of arrays with constants is called fixed-size array declaration. Fixed-size array declaration may pose two problems; either: Not enough capacity for the task at hand. Wasted space (too much capacity)
13
Winter 2004CS-1010 Dr. Mark L. Hornick 13 Java is not limited to fixed- size array declaration int size; int[] list; size= Integer.parseInt( JOptionPane.showInputDialog(null, “Enter the size of an array:”)); list = new int[size]; // size determined from user input
14
Winter 2004CS-1010 Dr. Mark L. Hornick 14 Any valid integer arithmetic expression is allowed for specifying the size of an array size = Integer.parseInt( JOptionPane.showInputDialog(null,””)); list = new int[size*size + 2* size + 5];
15
Winter 2004CS-1010 Dr. Mark L. Hornick 15 Arrays are not limited to primitive data types String[] monthName = { “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” }; An array of objects (e.g. Person) is declared and created just as an array of primitive data types is. Person[] person; person = new Person[20];
16
Winter 2004CS-1010 Dr. Mark L. Hornick 16 An array of Person objects after the array is created.
17
Winter 2004CS-1010 Dr. Mark L. Hornick 17 Arrays of Objects must be initialized Array elements are initially null. Person[20] person; // each person[k] is null Since each array element is an object, each must also be created. person[0] = new Person( “Me” ); person[1] = new Person( “You” ); …
18
Winter 2004CS-1010 Dr. Mark L. Hornick 18 The person array with one Person object added to it.
19
Winter 2004CS-1010 Dr. Mark L. Hornick 19 Arrays of Objects Robot[] clones; clones = new Robot[1000]; for(int i=0;i<clones.length; i++ ) { clones[i] = new Robot(); } // feed them all for(int i=0; i< clones.length; i++ ) { clones[i].eatMetal(); } // put Robot 300 to work: clones[299].washTheDishes();
20
Winter 2004CS-1010 Dr. Mark L. Hornick 20 Copying arrays vs copying array references Array elements are initially null unless initialized int[] list1 = {1,2,3,4,5}; int[] list2; // list2 is empty Sometimes it is useful to copy an array, but this is not the way to do it: list2 = list1; // not a “deep” copy!!! list2[3] = 10; // what is list1[3]???
21
Winter 2004CS-1010 Dr. Mark L. Hornick 21 Deep Copying Array elements are initially null unless initialized int[] list1 = {1,2,3,4,5}; int[] list2; // list2 is empty // make a new array that is the same length // as the list1, but IT IS EMPTY!!! list2 = new int[list1.length]; Here is how to copy list1 contents to list2: System.arraycopy(list1, 0, list2, 0, list1.length);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.