CS180 Recitation 25th/26th October, 2007 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Chapter 10 Introduction to Arrays
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Sections 1-4.
Arrays.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 2D Arrays.
Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 Arrays.
Recitation 3/27/2009 CS 180 Department of Computer Science, Purdue University.
©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.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
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.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Alice in Action with Java
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 10 Arrays and Collections.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
The Java Collections Framework (JCF) Introduction and review 1.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
 Pearson Education, Inc. All rights reserved Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Array Basics An array is a collection of data.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
Hashing as a Dictionary Implementation Chapter 19.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
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.
COMP More About Arrays Yi Hong June 05, 2015.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Welcome to CSE 143!.
ArrayLists.
Chapter 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Welcome to CSE 143!.
Arrays and Collections
Presentation transcript:

CS180 Recitation 25th/26th October, 2007 Department of Computer Science, Purdue University

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2 Announcements & Reminders Project 4 grades out Exam 2 on October 31 st, 7-8 PM Project 6 is due on the same day at 10 PM, so finish it early! Sign the academic integrity policy if you still haven’t.

Basics Arrays are an indexed collection of data values all of the same type e.g An array of length 20 -

Arrays of Primitive Data Types Array Declaration [ ] //variation 1 [ ] //variation 2 Array Creation = new [ ] Example

Arrays of Objects In Java, in addition to arrays of primitive data types, we can declare arrays of objects. An array of primitive data is a powerful tool, but an array of objects is even more powerful. The use of an array of objects allows us to model the application more cleanly and logically.

Array data type An array with elements of type T is a new data type represented as T[ ] int [ ] age; double salary[ ]; Person student[ ];  age is of type int[]  salary is of type double []  student is of type Person [] Each element of this array is of type T  age[0] is int type. salary[3] is double type  student [1] is a Person object.

Array Initialization It is possible to declare and initialize an array at the same time. It is also common to initialize an array using a for loop.

Index of an array An index for an array must be between 0 and length – 1 An index outside of this range will throw an ArrayIndexOutOfBoundsException This does not need to be caught, but the program will terminate if it’s not. Note:  Arrays have a public constant length which will tell you the length of an array Person[] a = new Person[17]; a.length == 17;

Array example 1 int[] values = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34}; String[] names = new String[5]; names[1] = “Jupiter”; names[3] = “Pluto”; for(i=0;i<values.length;++i) System.out.println(values[i] + “ “); System.out.println(names[values[values[3]]]);

Array Example2 Assume we have an array of double called numbers which has already been initialized. The average of the numbers is computed here- double sum = 0; for(int i = 0; i < numbers.length; i++){ sum += numbers[i]; } double average = sum/numbers.length;

Array Example3 – Array of objects public class Brady{ private String name; private int age; public Brady(String n, int a){ name = n; age = a; } public String getName(){ return name; } public int age(){ return age; } Brady[] bunch = new Brady[6]; bunch[0] = new Brady(“Bobby”, 8); bunch[1] = new Brady(“Cindy”, 7); bunch[2] = new Brady(“Peter”, 14); bunch[3] = new Brady(“Jan”, 13); bunch[4] = new Brady(“Marcia”, 15); bunch[5] = new Brady(“Greg”, 16); for(int i = 0; i < bunch.length; i++) { System.out.println(“Name: “ + bunch[i].getName() + “ Age: “ + bunch[i].getAge()); }

Variable Size Declaration In Java, we are not required to declare the size at compile time. The size of an array can be specified as a variable as shown below:

Passing Arrays to Methods When arrays are passed as arguments, the address of the array is copied to the parameter. Example: float arrayOne[] = new float[10];

Arguments and Return Values An array can be returned by a method. The return type must be an array in this case. public int[ ] doubleValues(int [ ] inArray) int[] array = new int[50]; int[] y = doubleValues(array);

Two-Dimensional Arrays Two-dimensional arrays are useful in representing tabular information. 2-dimensional arrays are usually represented in a row-column approach

Declaring and Creating a 2-D Array Declaration  [][] //variation 1  [][] //variation 2 Creation  = new [ ][ ] Example int[][] array; array = new int[4][5]; array

More on 2-d arrays A 2-d array is essentially an array of arrays The subarrays may have different lengths

2-d arrays example 1 final int ROWS = 5; final int COLUMNS = 5; char[][] board = new char[ROWS][COLUMNS]; for(int i = 0; i < board.length; i++){ for(int j=0; j<board[i].length; j++){ System.out.print(table[i][j] + “ “); } System.out.print(“\n”); } A B C D E F G H I J K L M N O P Q R S T U V W X Y Z # $ % ^ & * ?

2-d arrays example 2 Subarrays of different lengths Executing triangularArray = new double[4][ ]; for (int i = 0; i < 4; i++) triangularArray[i] = new double [i + 1]; results in an array that looks like:

Limitation of Arrays Once an array object is created, its size is fixed -- it cannot be changed. To overcome this, we can use Lists or Maps.

Lists and Maps The java.util standard package contains different types of classes for maintaining a collection of objects. These classes are collectively referred to as the Java Collection Framework (JCF). JCF includes classes that maintain collections of objects as sets, lists, or maps.

The List interface The List interface supports methods to maintain a collection of objects as a linear list. We can add to, remove from, and retrieve objects in a given list. A list does not have a set limit to the number of objects we can add to it. The ArrayList class uses an array to manage data. The LinkedList class uses a technique called linked-node representation.

List Methods Here are a few list methods:

List example 1 list = new ArrayList(); list.add("a"); // Append an element to the list // list - (a) list.add(0, "b"); // Insert an element at the head of the list // list - (b,a) int size = list.size(); // Get the number of elements in the list // size - 2

List example 1 (contd.) Object element = list.get(list.size()-1); // Retrieving the element at the end of the list // element returned- a list element = list.get(0); // Retrieving the element at the head of the list // element returned- b boolean b = list.remove("b"); // Remove the first occurrence of an element // b - true, list – (a) element = list.remove(0); // Remove the element at a particular index // list – (), element - a

JCF Maps JCF includes the Map interface that supports methods to maintain a collection of objects (key, value) pairs called map entries.

Map Methods Here are a few Map methods:

Map example Map map = new HashMap(); // hash table //Add key/value pairs to the map map.put("a", new Integer(1)); map.put("b", new Integer(2)); map.put("c", new Integer(3)); // Get number of entries in map int size = map.size(); // size - 3 // Adding an entry whose key exists in the map causes the new value to replace the old value Object oldValue = map.put("a", new Integer(9)); // oldValue - 1

Map example (contd) int value = map.get(“a”); // value - 9 // Remove an entry from the map and return the value of the removed entry oldValue = map.remove("c"); // 3