[PDF] [PDF] ArrayList - GitHub Pages

in Java, List is an interface, not a class A plain "ArrayList" accepts any kind of Object • When you "get" an Collections sort( list ); // sorts the menu Sort an 



Previous PDF Next PDF





[PDF] Java : les collections

AchrefELMOUELHIc G List Java List ArrayList LinkedList Vector H H: Research and Training 6 / 50 set(index, object) : remplace la valeur de l' élément d'indice index de la liste par object Collections sort(lettres); // pour trier la liste



[PDF] Les Collections - IGM

sort(T[] a, int fromIndex, int toIndex, Comparator



[PDF] High Performance Lists in Java - Magicwerkorg

stores an Integer object for each value ArrayList stores the elements contiguously in an array Sorting: the order of keys can be random or sorted ( using



[PDF] ArrayList - GitHub Pages

in Java, List is an interface, not a class A plain "ArrayList" accepts any kind of Object • When you "get" an Collections sort( list ); // sorts the menu Sort an 



[PDF] Chapitre 8 Collections en Java - Département dinformatique et de

http://developer java sun com/developer/onlineTraining/collections/Collection html ArrayList LinkedList Interfaces Map HashMap TreeMap Par contre, il n' y a pas d'implémentation de l'interface boolean remove(Object element); // Optional sort(List list,Comparator comp) ; trie une liste en utilisant un comparateur



[PDF] Compléments - MIS

En Java, l'outil javadoc (fourni avec le JDK) permet de générer une documentation HTML à partir boolean contains(Object o) List est implémentée par ArrayList, Vector, LinkedList, etc Par défaut, Collections sort( List l) utilise l'interface



[PDF] Interfaces Comparable et Comparator - Adrien Poupa

Sorts the specified array of objects into ascending order, according to the natural import java util List; import java util ArrayList; import java lang Comparable;



[PDF] Comparable interface - MIT OpenCourseWare

1 1 00 Lecture 34 Sorting Reading for next time: Big Java 15 4 Comparable interface • Used to define the natural order of objects Exam Test: Sort ArrayList

[PDF] arraylist object javascript

[PDF] arraylist remove object java

[PDF] arrays in java

[PDF] arrhenius equation activation energy

[PDF] arrhenius equation calculator

[PDF] arrhenius equation conductivity

[PDF] arrhenius equation derivation

[PDF] arrhenius equation example

[PDF] arrhenius equation graph

[PDF] arrhenius equation ln

[PDF] arrhenius equation notes

[PDF] arrhenius equation pdf

[PDF] arrhenius equation r

[PDF] arrhenius equation units

[PDF] arrima

ArrayList

James Brucker

Limitations of Arrays

You allocate space for array when you create it:

numWords = console.nextInt();

String [ ] words = new String[numWords];

What if you don't know the size of data in

advance? Example: reading words from a ifile, but you don't know how many words are in the ifile?

After you create an array, you cannot change the

size.

ArrayList

ArrayList food = new ArrayList( );

food.size(); // returns 0. Its empty food.add("Apple"); food.add("Banana"); food.size(); // returns 2

System.out.println( food.get(0) ); // Apple

System.out.println( food.get(1) ); // BananaArrayList is an alternative for variable size data yArrayList is an ordered collection of elements yArrayList grows and shrinks as needed! ycan add, delete, replace objects anywhere yArrayList is a class in Java List and ArrayList List is a basic data type (not a class). yin Java,

List is an interface, not a class

yyou cannot create "List" objects

ArrayList is a class that behaves like a

Listyyou can ignore "List" for now

Untyped ArrayList is a zoo

ArrayList list = new ArrayList( );

list.add( "Apple" ); // a string list.add( LocalDate.now() ); // LocalDate object list.add( new Double(3.14) ); // another object // Get something from arraylist

Object obj = list.get(1);

String fruit = (String) list.get(0);•A plain "ArrayList" accepts any kind of Object •When you "get" an element, it always returns type Object

Using a cast is dangerous

// Get Strings. Must cast the result

String fruit = (String) list.get(0);

// If result is not a String, an Exception occurs

String fruit2 = (String) list.get(1);

java.lang.ClassCastException: line xx•To get a "String" from ArrayList, we must cast the result to String. •What if the element is not a String?

Typed ArrayList

ArrayList fruit =

new ArrayList( ); list.add( "Apple" ); // a string list.add( "Orange" ); // string list.add( new Double(3.14) ); // Compile Error // Compiler will not allow to add a Double // No cast! Result is automatically String String s = list.get(1); // No cast! •Arraylist for String (only): ArrayList is called a type parameter. •Type can be any class name, but not primitive "ArrayList of String"

ArrayList means "ArrayList of Strings"

ArrayList means "ArrayList of Food"

Common operations

ArrayList fruit =

new ArrayList( ); list.add( "Apple" ); // add at end of list (0) list.add( "Orange" ); // add at end of list (1) list.add( 1, "Banana" ); // add at index 1 list.size( ); // 3 things in list list.get(1); // "Banana" was inserted list.get(2); // "Orange" was pushed down list.contains("Fig") // false list.remove("Apple") // remove first occurence list.get(0) // "Banana" Demo

View and inspect an ArrayList using BlueJ.

Notice what happens when number of items in

ArrayList increases.

Useful ArrayList Methods

int size( )returns # items in ArrayList add( T obj ) add an object to ArrayList (at end) add( int k, T obj )add obj at position k (push others down) T get(int index)get object at given index T remove(int index)delete item from ArrayList & return it clear( )remove all items from List set(int index, T obj) replace the object at index contains( T obj )"true" if obj is in ArrayList ensureCapacity(int size)make sure ArrayList can hold at least this many elements without resizing T = the type used to create ArrayList, can be String, Person,

Food,...

ensureCapacity( ) improved eiÌifiÌiciency when you are adding a lot of items to an Arraylist.

Working with ArrayList

Some useful methods

Iterate over all the elements

ArrayList menu = Restaurant.getMenu( );

for(int k=0; kSystem.out.println( list.get(k) ); }•Print everything in the restaurant menu •Print the menu using a for-each loop

ArrayList list = Restaurant.getMenu( );

for( String menuItem: menu ) {

System.out.println( menuItem );

Copying ArrayList to Array

ArrayList list = new ArrayList( ); ... read all the data and save in list // create an array large enough to store the data

String [ ] words = new String[ list.size( ) ];

// copy ArrayList to Array list.toArray( words );•Use an ArrayList to save data when you don't know how big the data set is. •list.toArray( array ) - copy to Array

Sorting

ArrayList list = Restaurant.getMenu( );

Collections.sort( list ); // sorts the menu Sort an ArrayList using the java.util.Collections class yCollections.sort( anyList ) yanyList must contain objects that are Comparable y String, Double, Long, Int, Date... y any class that has a compareTo method

Summary

ArrayList is a collection that:

 elements are ordered  can add, remove, or set elements at any pos'n  duplicate values are allowed  size grows/shrinks automatically

ArrayList is not an array.

More Information

Big Java, Chapter 7 or Core Java, Volume 1. Java Tutorial - has examples Java API documentation.quotesdbs_dbs20.pdfusesText_26