Array nDeclaratie nCreatie nOpvragen nWijzigen nLengte String [ ] a; a = new String[10]; ……a[5]…… a[5] = ……; …a.length… …is eigenlijk overbodig! ArrayList.

Slides:



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

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Hoofdstuk 13 Object-georiënteerd ontwerp. Methode-aanroep: Type van parameters void paint(Graphics g) { g.drawRect( ); class Graphics { void drawRect(int.
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.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
(c) University of Washington14-1 CSC 143 Java Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Computer Science 209 Software Development Java Collections.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
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.
Openen en Opslaan class Tekst : Form { } TextBox invoer; void lees (string naam) { } void schrijf (string naam) { } StreamReadersr = new StreamReader (naam);
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
MCQ Which is the correct syntax for placing the string "boat" into an ArrayList name recVehicles in position 3 (index 2) for the first time? a)recVehicles.set(3,
Computer Science 209 Software Development Inheritance and Composition.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Object Oriented Programming in Java Habib Rostami Lecture 7.
Java Collection Classes Com379PT
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Lecture 23:More on Interfaces
string versus array Klasse Type met speciale syntax Indexer -notatie
Software Development Java Collections
(H9.1) CirkelKlikker ARRAY int x, y; [ ] int n=0;
2e Deeltentamen Nagekeken werk ligt voor in de zaal (alfabetisch op achternaam) Neem het even mee, opg.13 wordt in het college besproken Discussies over.
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
Programming in Java Lecture 11: ArrayList
Introduction to Collections
(Java Collections Framework) AbstractSequentialList
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
slides created by Marty Stepp
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Presentation transcript:

Array nDeclaratie nCreatie nOpvragen nWijzigen nLengte String [ ] a; a = new String[10]; ……a[5]…… a[5] = ……; …a.length… …is eigenlijk overbodig! ArrayList a; a = new ArrayList(); …a.get(5)… a.set(5,…); …a.size()… array: oject dat een rij waarden bevat, met speciale notaties ArrayList: array “ingepakt” in een klasse, met standaard notaties nInvoegen nAchtervoegen a.add(5,…); ArrayList a; a = new ArrayList (); a.add(…);

Voorbeeld: array class Voorbeeld extends Applet { } void init() { } void actionPerformed(ActEv e) { } TextField in; void paint(Graphics g) { } in = new TextField(20); this.add(in); in.addActionListener(this); implements ActionListener String s = in.getText(); alles[n] = s; n++; this.repaint(); String [] alles; int n; alles = new String[100]; n = 0; for (int t=0; t<n; t++) { g.drawString( alles[t], 10, y ); y+=20; } int y = 50; if (n<100) { }

Voorbeeld: array class Voorbeeld extends Applet { } void init() { } void actionPerformed(ActEv e) { } TextField in; void paint(Graphics g) { } in = new TextField(20); this.add(in); in.addActionListener(this); implements ActionListener String s = in.getText(); alles[n] = s; n++; this.repaint(); String [] alles; int n; alles = new String[100]; n = 0; for (int t=0; t<n; t++) { g.drawString( alles[t], 10, y ); y+=20; } int y = 50; ArrayList ArrayList alles; alles = new ArrayList (); alles.add(s); alles.get(t) alles.size();t++)

Hoe is ArrayList gemaakt? class ArrayList { } Elem [] elems; int n; © ArrayList () { elems = new Elem[10]; n = 0; } Elem get (int p) { return elems[p]; } int size( ) { return n; } void add(Elem e) { } elems[n]=e; n++; if (n>=elems.length) vergroot(); private void vergroot() { } Elem [] kopie; kopie= new Elem[2*n]; for (int t=0; t<n; t++) kopie[t] = elems[t] elems = kopie;

Kan het ook anders? class ArrayList { } Elem [] elems; int n; © ArrayList () { elems = new Elem[10]; n = 0; } Elem get (int p) { return elems[p]; } int size( ) { return n; } void add(Elem e) { } elems[n]=e; n++; if (n>=elems.length) vergroot(); private void vergroot() { } Elem [] kopie; kopie= new Elem[2*n]; for (int t=0; t<n; t++) kopie[t] = elems[t] elems = kopie;

Ja: je kunt een List maken zonder dat een array nodig is class LinkedList { } …… © LinkedList () { …… } Elem get (int p) { …… } int size( ) { …… } void add(Elem e) { …… } null 3

Ja: je kunt een List maken zonder dat een array nodig is class LinkedList { } …… © LinkedList () { …… } Elem get (int p) { …… } int size( ) { …… } void add(Elem e) { …… } interface List { } int size(); boolean isEmpty(); void clear(); void add(Elem e) void add(int p, Elem e); void set(int p, Elem e); Elem get (int p); abstract class AbstractList implements List { } boolean isEmpty() { return this.size()==0; }

Package java.util Linked List Array List Abstract List interfaceabstracte klassediverse implementaties

Voorbeeld: List class Voorbeeld extends Applet { } void init() { } void actionPerformed(ActEv e) { } TextField in; void paint(Graphics g) { } in = new TextField(20); this.add(in); in.addActionListener(this); implements ActionListener String s = in.getText(); alles[n] = s; n++; this.repaint(); String [] alles; int n; alles = new String[100]; n = 0; for (int t=0; t<n; t++) { g.drawString( alles[t], 10, y ); y+=20; } int y = 50; ArrayList alles; alles = new ArrayList (); alles.add(s); alles.get(t) alles.size();t++) List alles; LinkedList Linked List Array List Abstract List

Varianten van List CollectionList Set interface Collection { void add(Elem x); void remove(Elem x); boolean contains(Elem x); int size(); } interface List extends Collection { Elem get(int n); void set(int n, Elem x); void add(int n, Elem x); } genummerd zonder dubbele Sorted Set gesorteerd interface Set extends Collection {} interface SortedSet extends Set { Elem first(); Elem last(); }

Linked List Array List Hash Set Tree Set Implementaties van Collections CollectionList Set Sorted Set Abstract Collection Abstract List Abstract Set

Voorbeeld: Collection class Voorbeeld extends Applet { } void init() { } void actionPerformed(ActEv e) { } TextField in; void paint(Graphics g) { } in = new TextField(20); this.add(in); in.addActionListener(this); implements ActionListener String s = in.getText(); alles[n] = s; n++; this.repaint(); String [] alles; int n; alles = new String[100]; n = 0; for (int t=0; t<n; t++) { g.drawString( alles[t], 10, y ); y+=20; } int y = 50; List alles; alles = new ArrayList (); alles.add(s); alles.get(t) alles.size();t++) Collection alles; TreeSet

Hoe doorloop je een Collection? nList nCollection for (int t=0; t<alles.size(); t++) doeIetsMet( alles.get(t) ); for (String s : alles) doeIetsMet( s ); declaratie typeexpr:naam opdracht for)(expropdrachtexpr ;; Speciale syntax voor specifieke situatie met bepaalde klasse