public void printList( ArrayList<Integer> data )
public void printList(ArrayList <Integer> data){ Iterator<Integer> it = data.iterator(); while(it.hasNext()){ System.out.print(it.next() + " "); } }
11, 4, 5, 12, 25, 18
Insert each key above, from left to right, into the hash table below using linear
probing to resolve collisions.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
25 | 18 | 11 | 4 | 5 | 12 |
Build a Map whose key is a sorted word (meaning that its characters are sorted in alphabetical order) and whose values are the word's anagrams.
String[] words = {"Fred","bob","Tom","Mark","john","Steve"};
sort them ignoring the case.
private class CompareIgnoreCase implements Comparator{ public void compare(Object o1, Object o2){ String str1 = ((String)o1).toLowerCase(); String str2 = ((String)02).toLowerCase(); return str1.compareTo(str2); } public void equals(Object obj){ String str1 = ((String)o1).toLowerCase(); String str2 = ((String)02).toLowerCase(); return str1.equals(str2); } } Arrays.sort(words, new CompareIgnoreCase());
String[] words = {"Fred@cmu.edu","Bob@yahoo.com","Tom@gmail.com", "Mark@ucla.edu","John@pit.edu", "Steve@microsoft.com"};
sort them by company/institution names.
private class CompareCompany implements Comparator{ public void compare(Object o1, Object o2){ String[] str1 = ((String)o1).split("@."); String[] str2 = ((String)02).split("@."); return str1[1].compareTo(str2[1]); } public void equals(Object obj){ String str1 = ((String)o1).split("@."); String str2 = ((String)02).split("@."); return str1[1].equals(str2[1]); } } Arrays.sort(words, new CompareCompany());