Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.

Similar presentations


Presentation on theme: "Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date."— Presentation transcript:

1 Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date

2 Goals By the end of this lesson, you should
know how to define and use an array know how to define and use an ArrayList be able to describe the difference between and array and ArrayList be able to describe the difference between pass-by-value and pass-by-reference

3 Arrays Array ArrayList Summary Arrays are a data structure that exist in all programming languages. They are best visualized as a table. The first element in a one-dimensional array is indexed zero [0]. So an array of size 6 has elements with indices 0, 1, 2, 3, 4, and 5: Like variables, array elements can be of any data type that Java recognises. E.g., int, double, String, or an object type (class)… This includes the ability to store other arrays. That is, arrays can have multiple dimensions. This is a 3x3 array of integers: ArrayList is a class in the java.util package that provides much of the standard functionality programmers want in an array. 1 2 3 4 5 17 42 -9 82 32 -37 1 2 -6 1 12 2 72 19 1 24 2 -8 16 1 54 2 7

4 Arrays ArrayList Summary Array
An array is defined the same way as the primitive type of its elements but with a set of [] after the type. It must also be instantiated, in one of two ways: 1. Give it an explicit length –elements are initialized to zero or null int[] myArray1 = new int[3]; System.out.println(myArray1[0]); 2. By initializing it when you declare it int[] myArray2 = {1,2,3}; You can ‘hard code’ references to cells – though you usually shouldn’t myArray1[0] = 1; myArray1[1] = 2; Multi-dimensional arrays can have different lengths in each dimension: int[][] my2dArray = {{1,2,3},{4,5}}; Array ArrayList Summary null is a special value that object variables, String variables etc. can contain. It means that the variable does not currently store an object.

5 Arrays – iterating(1) ArrayList Summary Array Old-fashioned counter
Two dimensional – nested for-loop: Array ArrayList Summary for (int i = 0;i< myArray1.length;i++){ System.out.printf("myArray1 position [%d] value %d, ", i, myArray1[i]); } for(int i=0;i<my2dArray.length;i++){ for(int j=0;j<my2dArray[i].length;j++){ System.out.printf( "my2dArray position [%d][%d] value %d, ", i, j, my2dArray[i][j]); } } Note: Java arrays have a length property. You can think of this as a read-only object (instance) variable that gets set when the size of the array is first set during initialization. Once set, the array length can’t be changed. To lengthen or shorten an array, you must create a new array of the desired size and copy the content of the old array across to the new one.

6 Arrays – iterating(2) ArrayList Summary Array
3. Using for loop with an iterator – note you cannot easily find the index of the current element // iterating over one-dimensional arrays for (int i: myArray2) { System.out.printf("myArray2 position ? value %d, ", i); } // iterating over two-dimensional arrays for (int[] i: my2dArray) { for (int j : i) { System.out.printf("my2dArray position ? value %d, ", j); Array ArrayList Summary Note: This is the “other type of for-loop” we promised earlier

7 Arrays utility class ArrayList Summary Array
java.util.Arrays has a number of useful methods. They are static so you don’t instantiate the class, but you do need the import. For example, you can sort an array myArray1 by calling: Arrays.sort(myArray1); There are others to search, fill, copy… Array ArrayList Summary

8 Arrays – passing to methods
Pass by value or reference. When we pass a primitive to a method we pass a copy of it. This is called pass-by-value. If the method changes the value in the method parameter, the original value is not change. Any other type (including arrays) is passed-by-reference. Under the hood, the method parameter stores the memory address of the original data. When the method changes the object or array that the parameter refers to, it changes the original! … // Pass by value or reference int num = 5; // num is a primitive PassByValueAndRef(num, myArray1); System.out.println("\n xx =" + num + " x " + myArray1[0]); } public static void PassByValueAndRef(int a, int b[]) { a = 88; b[0] = 77; Array ArrayList Summary What will the values be?

9 Arrays vrs ArrayList Arrays Are fixed in size
Have some basic attributes (such as array.length) Have a helper class in java.util.Arrays ArrayList Is a collection class managed by Java You can have arrays that grow and shrink It has lots more attributes and methods. Most people now use ArrayList rather than basic arrays. Array ArrayList Summary

10 Note you add the type twice
ArrayList declarations ArrayList is a ‘proper’ OO class 1. Import the library import java.util.ArrayList; …. 2. Declare an instantiate your ArrayList ArrayList<Double> numbers = new ArrayList<Double>(); 3. Use it! numbers.add(1.1111); numbers.get(i); numbers.set(0, 99.99); numbers.indexOf(num); numbers.remove(0); numbers.remove(index); ...and about 20 more, including overloaded methods! Array ArrayList Summary Note you add the type twice

11 %f: printf() code for a floating point type
ArrayList iteration You can use counted or enhanced iteration for an ArrayList: System.out.println("First loop:"); for (int i = 0; i<numbers.size(); i++){ System.out.printf("number %d is %f \n", i, numbers.get(i)); } System.out.println("Second loop\n"); for (double num : numbers){ System.out.printf("number %d is %f \n", numbers.indexOf(num), num); There’s a problem with the second loop. Can you spot it? Array ArrayList Summary %f: printf() code for a floating point type Note: Neat and practical printf() format code hint: %f will print your float or double with full precision – however many digits that may be! If you want show your customer that they have to pay $42.11 rather than $ , then you can specify two digits after the decimal point as %.2f

12 This is an overload of the previous PassByValueAndRef ()
Passing an ArrayList Array ArrayList Summary All objects, and ArrayList is used to instantiate an ArrayList object, are passed by reference! So our numbers object is passed by ref int num =1; PassByValueAndRef(num, numbers); // Note: num is a primitive, numbers is an object System.out.println("\n num = " + num " numbers[0] = " + numbers.get(0)); } static void PassByValueAndRef(int a, ArrayList<Double> b){ a = 88; b.set(0, 99.99); This is an overload of the previous PassByValueAndRef ()

13 What do we know DataType[] name
Array ArrayList Summary Java has two main ways to achieve and array DataType[] name ArrayList<Type> name = new ArrayList<Type>() Two ways to iterate through arrays for (i=0; i < arrayname.length; i++) for (int i : arrayname) Arrays and ArrayLists are passed-by-reference. This means the method called can change the original Utility methods Arrays has a helper class of static methods for ‘old’ style arrays (e.g., int[] intArray = {1,2,3};) ArrayList has its own methods built in We met a new for syntax for (type variable: collectionName)

14 Resources Do the revision exercises in chapter 6
Array ArrayList Summary D&D chapter 6 Homework Do the revision exercises in chapter 6 Use the debugger to watch what is in and out of scope when the method passByValueOrRef is called and which variables are changed where.

15 Next Lecture D&D Chapters 7 & 8 Objects (1)


Download ppt "Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date."

Similar presentations


Ads by Google