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

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
Arrays.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chapter 7.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
CSE 413, Autumn 2002 Programming Languages
Test 2 Review Outline.
Arrays Chapter 7.
4. Java language basics: Function
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 7: Working with Arrays
Computer Programming BCT 1113
John Hurley Cal State LA
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 7 Part 1 Edited by JJ Shepherd
Fifth Lecture Arrays and Lists
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
BIT115: Introduction to Programming
Arrays An Array is an ordered collection of variables
Java How to Program, Late Objects Version, 10/e
Can store many of the same kind of data together
Arrays versus ArrayList
EKT150 : Computer Programming
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Introduction To Programming Information Technology , 1’st Semester
Can store many of the same kind of data together
Object Oriented Programming in java
Arrays .
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Can store many of the same kind of data together
Single-Dimensional Arrays chapter6
CS 200 Objects and ArrayList
CSC 142 Arrays [Reading: chapter 12].
Arrays Arrays A few types Structures of related data items
Arrays.
CS 200 Objects and ArrayList
Arrays.
Week 7 - Monday CS 121.
Presentation transcript:

Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date

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

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

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.

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.

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

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

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?

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

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

%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 $42.10764251013345, then you can specify two digits after the decimal point as %.2f

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 ()

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)

Resources Do the revision exercises in chapter 6 Array ArrayList Summary D&D chapter 6 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html http://beginnersbook.com/2013/12/java-arraylist/ 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.

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