IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Data Structures ADT List
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
11-Jun-15 Generics. A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
Chapter 19 Java Data Structures
Java's Collection Framework
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
Java Collections Framework A presentation by Eric Fabricant.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
1 Unit 10 - Data Structures Objectives 1. Describe the usage of data structures. 2. Develop simple data structures. 3. Apply Java collections. 4. Apply.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
COLLECTIONS Byju Veedu s/Collection.html.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Collections. The Plan ● Why use collections? ● What collections are available? ● How are the collections different? ● Examples ● Practice.
Chapter 18 Java Collections Framework
Data structures Abstract data types Java classes for Data structures and ADTs.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections –data structures and Algorithms L. Grewe.
Data structures and algorithms in the collection framework 1.
Created by Terri Street Copyright, 2000  1,000,0001,000,000  500,000500,000  250,000250,000  125,000125,000  64,00064,000  32,00032,000  16,00016,000.
Collection 105 Yola. To store data in RAM Variables (name all the types with their length) Arrays (one, two or more) Collections and Maps.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
1 Java Programming II Collections. 2 Java Programming II Contents  Collections Framework  Collections of Objects  Iterable and Iterator  Comparable.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
University of Limerick1 Collections The Collection Framework.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Java Collection Classes Com379PT
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Using the Java Collection Libraries COMP 103 # T2
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Generics A Brief Review 16-Nov-18.
Containers ב Java אליהו חלסצ'י תכנות מתקדם תרגול מספר 3
Collections Framework
CSE 143 Lecture 5 References and Linked Nodes
Podcast Ch21f Title: HashSet Class
Presentation transcript:

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki2 Class Extent – What is it? Class extent is an actual set of all occurences of a class. Class extent is implemented as a special data structure which stores all objects of this class.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki3 How to implement class extent? Using static array Using collections Using a separate class

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki4 Static array implementation One of the methods is to use a static array which stores all occurences of particular class. Using this method we should remember that the array which is storing the object list should be updated every time the class is created or deleted. In this method, class extent functions ( those which works on every class instance ) should be static. Code example:

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki5 Code example: static array class Test { private static final int MAX_SIZE = 100; private static Test[] extension = new Test[MAX_SIZE]; private static int objectCount = 0; private int number; public Test(int number) { this.number = number; extension[objectCount] = this; objectCount++; } public int getNumber() { return number; } public static int sum() { int sum = 0; for (int i = 0; i < objectCount; i++) { sum += extension[i].getNumber(); } return sum; } public static void remove(Test t) { for (int i = 0; i < objectCount; i++) { if (extension[i] == t) { extension[i] = extension[objectCount - 1]; extension[objectCount] = null; objectCount--; break; }

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki6 Code example: static array (2) public class Demo { public static void main(String[] args) { Test test1 = new Test(8); Test test2 = new Test(31); Test test3 = new Test(419); System.out.println("The sum is " + Test.sum()); Test.remove(test1); Test.remove(test2); Test.remove(test3); }

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki7 Collections A collection is a kind of data structure which ( just like arrays ) is used to store data and perform some operations on it. In difference from arrays it is a class which includes some methods allowing some basic operations on stored data such as adding, deleting, viewing. Collection classes in Java: HashSet, TreeSet, ArrayList, LinkedList, HashMap, TreeMap, WeakHashMap, HashTable, Vector, Stack... Example: ArrayList // creating collection ArrayList collection = new ArrayList(); // adding object to collection collection.add(object); // deleting object from collection collection.remove(id); or remove(object);

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki8 Collections – iterators Viewing collections is performed using iterators. Iterators enables access to each element in a collection in specific order (generally by order of inserting the elements into the queue) Example: Iterator ArrayList collection = new ArrayList(); Integer num1 = new Integer(2); Integer num2 = new Integer(5); Integer num3 = new Integer(8); collection.add(num1); collection.add(num2); collection.add(num3); Iterator i = collection.iterator(); while ( i.hasNext() ) { System.out.println ( (Integer)i.next() ) ; }

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki9 Class extension as object collection We may use static collection as class extension, which causes much less amount of code lines and makes application working faster. Use of object collection joins the advantages of using static array and dynamic allocated static array.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki10 Code example: collection class Test { private static ArrayList extension = new ArrayList(); private int number; public Test(int number) { this.number = number; extension.add(this); } public int getNumber() { return number; } public static int sum() { int sum = 0; Iterator it = extension.iterator(); while (it.hasNext()) { Test t = (Test) it.next(); sum += t.getNumber(); } return sum; } public static void remove(Test t) { extension.remove(t); }

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki11 Implementaion of class extension using a separate class Attendance of class extension by seperate class is organised in such a way that another (seperate) class (so called „container”) is responsible for object creating and management. Container contains its own methods that enable adding and deleting objects in another class.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki12 Advantages of class extension using a separate class The greatest advantage is that programmer has control over creating and deleting class objects Such classes have great functionality like sorting and many other

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki13 Disadvantages of class extension using a separate class It is the most complicated way of attendance of class extension

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki14 Code example: separate class class Test { int number; public Test(int number) { this.number = number; } class TestContainer { private static ArrayList tests = new ArrayList(); public void addTest(int number) { Test t = new Test(number); tests.add(t); } public int getTestsCount() { return test.count(); } public int sum() { int sum = 0; Iterator it = tests.iterator(); while (it.hasNext()) { Test t = (Test) it.next(); sum += t.getNumber(); } return sum; } public void remove(Test t) { tests.remove(t); } public void removeAll() { test.clear(); }

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki15 Code example: separate class (2) public class Demo { public static void main(String[] args) { new Demo(); } public Demo() { TestContainer container = new TestContainer(); container.add(5); container.add(8); container.remove(1); System.out.println("The sum is " + container.sum()); }

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki16 Homework Implement the extension of class Person using static array (Person has first name, last name and age). Write a method to find the youngest person and calculate average age.