Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Similar documents
36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java Collection Framework

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.


LAB 13: ARRAYS (ONE DIMINSION)

Generic Collections CSC Spring 2019 Howard Rosenthal

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

Generics. IRS W-9 Form

Objects and Iterators

CONTAİNERS COLLECTİONS

Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING)

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction to Computer Science I

PIC 20A Number, Autoboxing, and Unboxing

Java Collections Framework: Interfaces

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

Introduction to Programming Using Java (98-388)

CS Ananda Gunawardena

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lecture 9: Lists. MIT-AITI Kenya 2005

Computational Expression

The Java Collections Framework and Lists in Java Parts 1 & 2

Fundamental language mechanisms

9/16/2010 CS Ananda Gunawardena

Formatting Output & Enumerated Types & Wrapper Classes

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

MIT AITI Lecture 18 Collections - Part 1

CMSC 202. Containers

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

A variable is a name for a location in memory A variable must be declared

JAVA WRAPPER CLASSES

CMSC 202H. Containers and Iterators

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Computer Science 210 Data Structures Siena College Fall 2018

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

Object Oriented Methods : Deeper Look Lecture Three

List... NOTE We use List as illustrative example of collections Study the others from textbook!

C20a: Selected Interfaces in Java API

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Csci 102: Sample Exam

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Chapter 2: Using Data

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

ABSTRACT DATA TYPES: COLLECTIONS, LISTS, SETS, MAP, QUEUES. Thursday, June 30, 2011

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

USING LIBRARY CLASSES

Computational Expression

What is the Java Collections Framework?

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

CS Programming I: ArrayList

Computational Expression

Java Language Features

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Lecture 4. The Java Collections Framework

Java Collections Framework reloaded

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013

Java Collections Framework. 24 April 2013 OSU CSE 1

Using Classes and Objects. Chapter

Summer Final Exam Review Session August 5, 2009

The Java Collections Framework. Chapters 7.5

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Java Collections Framework

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Dynamic Data Structures and Generics

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CS313D: ADVANCED PROGRAMMING LANGUAGE

Full file at

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CS2110 Assignment 3 Inheritance and Trees, Summer 2008

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Section Lists and Sets

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

Objec&ves. Packages Collec&ons Generics. Sept 28, 2016 Sprenkle - CSCI209 1

EXAMINATIONS 2016 TRIMESTER 2

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

Outline. iterator review iterator implementation the Java foreach statement testing

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Today: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1

CS162: Introduction to Computer Science II

Java Collections. Wrapper classes. Wrapper classes

Java Collections. Engi Hafez Seliem

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSSE 220. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysListPractice from SVN

Collections, Maps and Generics

[Ref: Core Java Chp 13, Intro to Java Programming [Liang] Chp 22, Absolute Java Chp 16, docs.oracle.com/javase/tutorial/collections/toc.

ArrayLists. Chapter 12.1 in Savitch

CSSE 220 Day 8. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysAndLists from SVN

Java 1.8 Programming

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Java Programming with Eclipse

Transcription:

Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved.

Up to here

Not included in program

Java collections framework prebuilt data structures interfaces and methods for manipulating those data structures

A collection is a data structure actually, an object that can hold references to other objects. Usually, collections contain references to objects that are all of the same type. Figure 20.1 lists some of the interfaces of the collections framework. Package java.util.

Each primitive type has a corresponding type-wrapper class (in package java.lang). Boolean, Byte, Character, Double, Float, Integer, Long and Short. Each type-wrapper class enables you to manipulate primitive-type values as objects. Collections cannot manipulate variables of primitive types. They can manipulate objects of the type-wrapper classes, because every class ultimately derives from Object.

Each of the numeric type-wrapper classes Byte, Short, Integer, Long, Float and Double extends class Number. The type-wrapper classes are final classes, so you cannot extend them. Primitive types do not have methods, so the methods related to a primitive type are located in the corresponding type-wrapper class.

A boxing conversion converts a value of a primitive type to an object of the corresponding type-wrapper class. An unboxing conversion converts an object of a type-wrapper class to a value of the corresponding primitive type. These conversions can be performed automatically (called autoboxing and auto-unboxing). Example: // create integerarray Integer[] integerarray = new Integer[ 5 ]; // assign Integer 10 to integerarray[ 0 ] integerarray[ 0 ] = 10; // get int value of Integer int value = integerarray[ 0 ];

Interface Collection is the root interface from which interfaces Set, Queue and List are derived. Interface Set defines a collection that does not contain duplicates. Interface Queue defines a collection that represents a waiting line. Interface Collection contains bulk operations for adding, clearing and comparing objects in a collection.

A Collection can be converted to an array. Interface Collection provides a method that returns an Iterator object, which allows a program to walk through the collection and remove elements from the collection during the iteration. Class Collections provides static methods that search, sort and perform other operations on collections.

A List (sometimes called a sequence) is a Collection that can contain duplicate elements. List indices are zero based. In addition to the methods inherited from Collection, List provides methods for manipulating elements via their indices, manipulating a specified range of elements, searching for elements and obtaining a ListIterator to access the elements. Interface List is implemented by several classes, including ArrayList, LinkedList and Vector. Autoboxing occurs when you add primitive-type values to objects of these classes, because they store only references to objects.

Class ArrayList and Vector are resizable-array implementations of List. Inserting an element between existing elements of an ArrayList or Vector is an inefficient operation. A LinkedList enables efficient insertion (or removal) of elements in the middle of a collection. The primary difference between ArrayList and Vector is that Vectors are synchronized by default, whereas ArrayLists are not.

List method add adds an item to the end of a list. List method size returns the number of elements. List method get retrieves an individual element s value from the specified index. Collection method iterator gets an Iterator for a Collection. Iterator- method hasnext determines whether a Collection contains more elements. Returns true if another element exists and false otherwise. Iterator method next obtains a reference to the next element. Collection method contains determine whether a Collection contains a specified element. Iterator method remove removes the current element from a Collection.

Class Collections provides several highperformance algorithms for manipulating collection elements. The algorithms (Fig. 20.5) are implemented as static methods.

Method sort sorts the elements of a List The elements must implement the Comparable interface. The order is determined by the natural order of the elements type as implemented by a compareto method. Method compareto is declared in interface Comparable and is sometimes called the natural comparison method. The sort call may specify as a second argument a Comparator object that determines an alternative ordering of the elements.

The Comparator interface is used for sorting a Collection s elements in a different order. The static Collections method reverseorder returns a Comparator object that orders the collection s elements in reverse order.

Figure 20.8 creates a custom Comparator class, named TimeComparator, that implements interface Comparator to compare two Time2 objects. Class Time2, declared in Fig. 8.5, represents times with hours, minutes and seconds. Class TimeComparator implements interface Comparator, a generic type that takes one type argument. A class that implements Comparator must declare a compare method that receives two arguments and returns a negative integer if the first argument is less than the second, 0 if the arguments are equal or a positive integer if the first argument is greater than the second.

Method shuffle randomly orders a List s elements.

Collections method reverse reverses the order of the elements in a List Method fill overwrites elements in a List with a specified value. Method copy takes two arguments a destination List and a source List. Each source List element is copied to the destination List. The destination List must be at least as long as the source List; otherwise, an IndexOutOfBoundsException occurs. If the destination List is longer, the elements not overwritten are unchanged. Methods min and max each operate on any Collection. Method min returns the smallest element in a Collection, and method max returns the largest element in a Collection.

Collections method addall takes two arguments a Collection into which to insert the new element(s) and an array that provides elements to be inserted. Collections method frequency takes two arguments a Collection to be searched and an Object to be searched for in the collection. Method frequency returns the number of times that the second argument appears in the collection. Collections method disjoint takes two Collections and returns true if they have no elements in common.

Student Poll. Figure 7.8 contains an array of survey responses that s hard coded into the program. Suppose we wish to process survey results that are stored in a file. This exercise requires two separate programs. First, create an application that prompts the user for survey responses and outputs each response to a file. Use a Formatter to create a file called numbers.txt. Each integer should be written using method format. Then modify the program in Fig. 7.8 to read the survey responses from numbers.txt. The responses should be read from the file by using a Scanner. Use method nextint to input one integer at a time from the file. The program should continue to read responses until it reaches the end of the file. The results should be output to the text file "output.txt".

Figure 7.8 uses arrays to summarize the results of data collected in a survey: Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (where 1 means awful and 10 means excellent). Place the 40 responses in an integer array, and summarize the results of the poll. Array responses is a 40-element int array of the survey responses. 11-element array frequency counts the number of occurrences of each response (1 to 10). Each element is initialized to zero by default. We ignore frequency[0].

End of the course Hope you have enjoyed the course Good luck and have fun! (C) 2010 Pearson Education, Inc. All rights reserved.