Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.

Similar presentations


Presentation on theme: "Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session."— Presentation transcript:

1 Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session

2 Revision of session 8 Java is distributed with a collection of ready-to-use classes, called the Core Class Libraries. A full list of the classes available can be found on the Java Core Class Libraries documentation page: http://download.oracle.com/javase/1.4.2/docs/api/overview-summary.html To use external classes: import classname; String class contains many methods for manipulating Strings. Math class contains mathematical constants and operations. Many String and Math methods are static!

3 Session 9 - aims & objectives An introduction to arrays. Useful for storing large amounts of similarly structured data. Revision of Sessions 1-8.

4 Arrays – 1 These are like lists or a tables of data. Contains only one data type e.g. integers, Strings etc. Array size must be defined before you use it i.e. how many elements (or list items) do you want to have in it? Arrays start counting from 0 e.g. element 0, element 1, element 2 etc. This is called the index of the element. Each element in the array can be addressed by its index number.

5 Arrays – 2 To create an array, we need to specify the data type and the name of the array, and then use the new keyword in the usual way: int[] year = new int[3]; Square brackets signify that we are refering to an array. Can think of “int[]” as a bit like it’s own data type int[] = “an array of ints” Above, 3 is the number of elements in the array. Can refer to elements using square brackets again: year[0]=1980; year[1]=1990; year[2]=2000;

6 Arrays – 3 You can actually do either of the following: int[] year = new year[3]; int year[] = new year[3]; Both lines do the same thing. However, in the first line “int[]” stands out more clearly as an array of ints! We’ll try to stick to the syntax of the first line, but you might see either in general.

7 Arrays – 4 Here’s some typical array coding: int[] year = new year[3]; int[1]=2011; int[2]=2010; int[3]=2009; What will happen when you compile this code?

8 Arrays – 5 Use of arrays is quite limited. Can only store one data type. Need to know the max number of elements to store in advance. If this is inconvenient, we can use other approaches… We’ll see an example in Session 15.

9 Example code: Take a look at example codes ex9_01 and ex9_02, which demonstrate the use of arrays. You can try exercise 9.1 after today’s session, to practice working with arrays.

10 Revision task – Bubble sort! Task: Write a program which asks the user to enter a list of numbers, stores these numbers in an array, sorts them into ascending numerical order, and prints out the sorted list. There are many ways to sort a list of numbers. We will use the Bubblesort algorithm, which is easy to understand, but quite inefficient. The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. This process is repeated until it goes all the way through the list without swapping any items (i.e. all items are in the correct order).

11 Coming up in Session 10... Constructors Useful methods for creating objects more quickly. The String class revisited Creating Strings using constructors. Note: Strings are a little bit like char[]’s, except Strings are objects!


Download ppt "Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session."

Similar presentations


Ads by Google