Professional Documents
Culture Documents
Colecciones
1
8/18/2009
2
8/18/2009
• Buscar por un Object y retornar su posición • Check to see if the List is empty:
3
8/18/2009
• Two Set objects are equal if they contain the same • Difference of two sets (A – B) – Use:
elements. • boolean removeAll(Collection c)
4
8/18/2009
• // you must create iterator for set • You may want to use a set to get rid of duplicates,
• Iterator<String> iter2 = firstnames.iterator(); then put the items in an ArrayList and sort them!
• // use iterator to print elements in set • Problem:
– Often data comes in the form of an array
• while (iter2.hasNext())
– How
o do we e go from
o array
a ay to ArrayList
ay st oor TreeSet?
eeSet
• { • Problem:
• System.out.println(iter2.next()); – Often we are required to return an array
• } – How do we go from a Collection such as an ArrayList or TreeSet to
an array?
• Can do it the “hard” way with loops or iterators:
• OR:
• For arrays of objects (such as Strings) use the asList • Collections such as ArrayLists and TreeSets have a
method in the Arrays class. toArray method
• This returns a fixed-size list backed by the specified – This returns an array
array – Sintax: a bit awkward
• Pass this into the constructor of your ArrayList or set • Example
• TreeSet<String> wordset = new TreeSet<String>();
• Example
• . . .
• String[] words = String[N];
• String[] words =
• . . . • (String[]) wordset.toArray(new String[0]);
• TreeSet<String> wordset = new • or
• TreeSet<String>(Arrays.asList(words)); • return (String[]) wordset.toArray(new String[0]);
5
8/18/2009
6
8/18/2009