Download presentation
Presentation is loading. Please wait.
Published bySamuel McFarland Modified over 10 years ago
1
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2013 Week 9: Arrays
2
Java Lab 8, Ex 1: RepeatString Implement a class which contains the method main and the method public static String repeat(String str, int n) which returns the string str repeated n times. Call the method repeat from main. 8 March 2013Birkbeck College, U. London2
3
Code for Exercise 2 (1) /** * Program to test the method repeat which prints out a * given string, repeated a given number of times. * @author S.J. Maybank * @version 22 February 2011 */ public class RepeatString { // main goes here // repeat goes here } 8 March 2013Birkbeck College, U. London3
4
Code for Exercise 2 (2) public static void main(String[] args) { String str1 = repeat("Hello World", 3); String str2 = repeat("Hello World", 0); String str3 = repeat("H", 10); System.out.println("Examples of the output from the method repeat"); System.out.println(str1); System.out.println(str2); System.out.println(str3); } 8 March 2013Birkbeck College, U. London4
5
Code for Exercise 2 (3) public static String repeat(String str, int n) { String result = ""; for(int i = 1; i <= n; i++) { result = result+str; } return result; } 8 March 2013Birkbeck College, U. London5
6
Java Lab 8, Ex 2: ReadDouble Implement the method public static double readDouble(String prompt) which displays the string prompt, followed by a space, then reads in a floating point number and returns it. 8 March 2013Birkbeck College, U. London6
7
Code for Exercise 3 (1) import java.util.Scanner; /** * Display a prompt string and then read in a number of type double. * @author S.J. Maybank * @version 22 February 2011 */ public class ReadDouble { // main goes here // readDouble goes here } 8 March 2013Birkbeck College, U. London7
8
Code for Exercise 3 (2) public static void main(String[] args) { double r = readDouble(Input a number of type double:"); System.out.println("The input number is "+r); } 8 March 2013Birkbeck College, U. London8
9
Code for Exercise 3 (3) public static double readDouble(String prompt) { Scanner in = new Scanner(System.in); System.out.print(prompt+" "); return in.nextDouble(); } 8 March 2013Birkbeck College, U. London9
10
Overview Nature of an array Declaration of an array Array indexing Partially filled arrays Array algorithms See Java for Everyone, Ch. 6 8 March 2013Birkbeck College, U. London10
11
Arrays A mechanism for collecting together multiple values. A way of allocating names to multiple variables. 8 March 2013Birkbeck College, U. London11
12
Array Declaration and Initialisation double[] data = {32, 54, 67, 5}; 8 March 2013Birkbeck College, U. London12 type of array variable name of array variable list of initial values Names of variables: data[0], data[1], data[2], data[3] The numbers 0, 1, 2, 3 are indices
13
Example data[3] = 35; System.out.println(data[3]); // prints 35 /* Element: data[3] Value of the index: 3 Value of the element data[3]: 35 Each element can be used like any variable of type double. */ 8 March 2013Birkbeck College, U. London13
14
Array Indexing Array indexing begins with 0 The length of the array data is data.length Array indexing ends with data.length-1 for(int i = 0; i < data.length; i++) { System.out.println(data[i]); } 0 March 2013Birkbeck College, U. London14
15
Bounds Error data[data.length] = 4; //error data[-1] = 3; // error /* A bounds error is automatically detected at run time. It is not detected at compile time. */ 8 March 2013Birkbeck College, U. London15
16
Array References int[] scores = {10, 9, 7, 4, 5}; int[] values = scores; scores[3] = 10; System.out.println(values[3]); // prints 10! /* The array variable is a pointer to the place in memory where the array is stored. */ 8 March 2013Birkbeck College, U. London16
17
The Array Variable as a Pointer 8 March 2013Birkbeck College, U. London17 array stored in memoryscores values 109745 The value of the variable score is a pointer to the array. The value of the variable values is also a pointer to the array
18
Array Declaration and Initialisation (2) double[] data = new double[4]; 8 March 2013Birkbeck College, U. London18 type of array variable name of array variable element type length The elements of the array data are automatically assigned the value 0, but do not rely on this!
19
Declaring Arrays 8 March 2013Birkbeck College, U. London19 int[] numbers = new int[10];An array of 10 integers. All elements are initialised with 0 final int LENGTH = 10; int[] numbers = new int[LENGTH]; Use a named constant int[] squares = {0, 1, 4, 9, 16}; String[] friends = {"Em", "Bob", "Sue"};An array of three strings double[] data = new int[10];Error: a variable of type double[] cannot be assigned a value of type int[]
20
Partially Filled Arrays In a partially filled array, only part of the array is used. A companion variable keeps count of the number of array elements that have been used. 8 March 2013Birkbeck College, U. London20
21
Example of a Partially Filled Array int currentSize = 0; // companion variable Scanner in = new Scanner(System.in); while(in.hasNextDouble()) // check for further input { if(currentSize < data.length) { data[currentSize] = in.nextDouble(); currentSize++; } 8 March 2013Birkbeck College, U. London21
22
Printing a Partially Filled Array for(int i = 0; i < currentSize; i++) { System.out.println(data[i]); } 8 March 2013Birkbeck College, U. London22
23
Array Algorithm 1 /* find the sum and the average value of the elements in the array data. */ double total = 0; for(i = 0; i < data.length; i++) { total = total + data[i]; } double average = 0; if(data.length>0){average = total/data.length;} 8 March 2013Birkbeck College, U. London23
24
Array Algorithm 2 /* Find the maximum and the minimum of the elements in the non-empty array data. */ double largest = data[0], smallest = data[0]; for(int i = 0; i < data.length; i++) { if(data[i] > largest){largest = data[i];} if(data[i] < smallest){smallest = data[i];} } 8 March 2013Birkbeck College, U. London24
25
Array Algorithm 3 /* Linear search. */ int searchedValue = 100, pos = 0; boolean found = false; while (pos < values.length && !found) { if(values[pos] == searchedValue) { found = true; } else { pos++; } 8 March 2013Birkbeck College, U. London25
26
Array Algorithm 3 (Coda) if(found) { System.out.println(Found at position+pos); } else { System.out.println(Not found); } 8 March 2013Birkbeck College, U. London26
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.