1 / 13

H 22. COLLECTIONS FRAMEWORK. 1. INLEIDING.

H 22. COLLECTIONS FRAMEWORK. 1. INLEIDING. Collections framework Is een verzameling van data structuren, interfaces en algoritmen Meest voorkomende datastructuren gestandaardiseerd en efficient geïmplementeerd. Voorbeeld van herbruikbare code. 2. OVERZICHT COLLECTIONS FRAMEWORK.

Download Presentation

H 22. COLLECTIONS FRAMEWORK. 1. INLEIDING.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. H 22. COLLECTIONS FRAMEWORK. 1. INLEIDING. • Collections framework • Is een verzameling van data structuren, interfaces en algoritmen • Meest voorkomende datastructuren gestandaardiseerd en efficient geïmplementeerd. • Voorbeeld van herbruikbare code JAVA

  2. 2. OVERZICHT COLLECTIONS FRAMEWORK • Collection • Is een data structuur (object) die referenties naar andere objecten bijhoudt. • Collections framework • Interfaces die de bewerkingen declareren voor verschillende collection types en verschillende implementaties (classes) daarvan. • Behoren tot het package java.util • Collection • Set • List • Map JAVA

  3. 3. CLASS ARRAYS • Class Arrays • Voorziet static methoden voor manipulatie van arrays • Voorziet “high-level” methoden • Methode binarySearch voor het zoeken in geordende arrays • Methode equals voor het vergelijken van arrays • Method fill voor waarden in te brengen in ee, array • Method sort voor sorteren JAVA

  4. 3. CLASS ARRAYS : Voorbeeld methoden. import java.util.*; public class UsingArrays { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double doubleValues[] = { 8.4, 9.3, 0.2, 7.9, 3.4}; private int filledInt[], intValuesCopy[]; public UsingArrays()// initialize arrays { filledInt = new int[ 10 ]; intValuesCopy = new int[ intValues.length ]; // vul filledInt volledig op met 7’s Arrays.fill( filledInt, 7 ); // sorteer doubleValues in oplopende volgorde Arrays.sort( doubleValues ); // copy array intValues naar array intValuesCopy System.arraycopy( intValues, 0, intValuesCopy, 0, intValues.length ); } JAVA

  5. 3. Methode arraycopy // copy array intValues naar array intValuesCopy System.arraycopy( intValues, 0, intValuesCopy, 0, intValues.length ); static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) Parameters: src - de source array. srcPos - start positie in de source array. dest - de destination array. destPos - start positie in de destination array. length – aantal elementen die gekopieerd moeten worden. Werpt: IndexOutOfBoundsException – indien de grenzen van srcofdest overschreden worden. ArrayStoreException – indien het type van de array srcniet in dest geplaatst kan worden. NullPointerException – indien srcofdest null is. JAVA

  6. 3. CLASS ARRAYS : Voorbeeld methoden. public void doiets() { //vergelijk de inhoud van beide arrays boolean b = Arrays.equals( intValues, intValuesCopy ); //... // element opzoeken int location= Arrays.binarySearch( intValues, value ); //indien value = 5 returns 4 //indien value = 8763 returns –7 // -7 = de indexwaarde_in_geval_van_invoegen*(-1) -1 //... } //... } JAVA

  7. 3. CLASS ARRAYS : overloaded methoden • static int binarySearch(int[] a, int key) • static int binarySearch(double[] a, double key) • Idem voor char, byte, short, long en float • static int binarySearch(Object[] a, Object key) • static int binarySearch(Object[] a, Object key, Comparator c) zie paragraaf 6. • static boolean equals(int[] a, int[] a2) • Idem voor char, byte, short, long, float en double • static boolean equals(Object[] a, Object[] a2) JAVA

  8. 3. CLASS ARRAYS : overloaded methoden • static void fill(int[] a, int val) • static void fill(int[] a, int fromIndex, int toIndex, int val) • IllegalArgumentException – if fromIndex > toIndex • ArrayIndexOutOfBoundsException – if fromIndex < 0 of toIndex > a.length • Idem voor char, byte, short, long, float, double en object JAVA

  9. 3. CLASS ARRAYS : overloaded methoden • static void sort(int[] a) • static void sort(int[] a, int fromIndex, int toIndex) • IllegalArgumentException – if fromIndex > toIndex • ArrayIndexOutOfBoundsException – if fromIndex < 0 of toIndex > a.length • Idem voor char, byte, short, long, float en double • static void sort(Object[] a) • static void sort(Object[] a, Comparator c) • static void sort(Object[] a, int fromIndex, int toIndex) • static void sort(Object[] a, int fromIndex, int toIndex, Comparator c) zie paragraaf 6. JAVA

  10. 3. CLASS ARRAYS : static methode asList static methode asList laat toe een Arrays object als een List (zie verder) te doorlopen. Wijzigen op de elementen via de List view zijn effectief in het Arrays object, en vise versa. Opm: de List heeft een vaste lengte (geen add of remove!). Voorbeeld: import java.util.*; public class UsingAsList { private static final String values[] = { "red", "white", "blue" }; private List list; // initialiseer List en wijzig (set methode) de waarde // op locatie 1 public UsingAsList() { list = Arrays.asList( values ); // get List list.set( 1, "green" );// wijzig een waarde } JAVA

  11. 3. CLASS ARRAYS : static methode asList // output via List en via array public void printElements() { System.out.print( "List elements : " ); for ( int count = 0; count < list.size(); count++ ) System.out.print( list.get( count ) + " " ); System.out.print( "\nArray elements: " ); for ( int count = 0; count < values.length; count++ ) System.out.print( values[ count ] + " " ); System.out.println(); } public static void main( String args[] ) { new UsingAsList().printElements(); } } List elements : red green blue Array elements: red green blue JAVA

  12. 4. INTERFACE COLLECTION EN CLASS COLLECTIONS. • Interface Collection • Bevat bulk operations (op de volledige collection) • Adding, clearing, comparing en retaining objects in de collection • Interfaces Set en List extend interface Collection • Voorziet een Iterator, die ook elementen kan verwijderen. • Class Collections • Voorziet static methoden die collections manipuleren • Polymorfisme wordt hierbij ondersteund JAVA

  13. 5. LIST INTERFACE. Interface Collection Interface List Vector, ArrayList en LinkedList zijn implementatie klassen van interface List. JAVA

More Related