Commit | Line | Data |
---|---|---|
5687dc10 JB |
1 | import java.util.ArrayList; |
2 | import java.util.ListIterator; | |
3 | ||
4 | public class Utils { | |
5 | public static void displayArrayList(ArrayList arrayList) { | |
6 | ListIterator iter = arrayList.listIterator(); | |
d252f672 | 7 | int i = 0; |
5687dc10 | 8 | while (iter.hasNext()) { |
d252f672 | 9 | System.out.println(i + " " + iter.next()); |
5687dc10 JB |
10 | i++; |
11 | } | |
12 | } | |
13 | ||
14 | public static void computeDistanceFrom(Person personFrom, ArrayList<Person> personArrayList) { | |
15 | if (!personArrayList.contains(personFrom)) | |
16 | personArrayList.add(personFrom); | |
17 | ListIterator<Person> iter = personArrayList.listIterator(); | |
18 | while (iter.hasNext()) { | |
19 | Person personCursor = iter.next(); | |
20 | personCursor.setDistanceFromReference(personFrom.distanceTo(personCursor)); | |
21 | } | |
22 | } | |
ac6c3ea2 | 23 | |
ac6c3ea2 | 24 | /** |
ac6c3ea2 JB |
25 | * @param a |
26 | * @param b | |
27 | * @param c | |
28 | * @return | |
29 | */ | |
30 | private static int minimum(int a, int b, int c) { | |
31 | return Math.min(Math.min(a, b), c); | |
32 | } | |
33 | ||
34 | /** | |
ac6c3ea2 JB |
35 | * @param lhs |
36 | * @param rhs | |
37 | * @return | |
38 | */ | |
39 | public static int computeLevenshteinDistance(CharSequence lhs, CharSequence rhs) { | |
40 | int[][] distance = new int[lhs.length() + 1][rhs.length() + 1]; | |
41 | ||
42 | for (int i = 0; i <= lhs.length(); i++) | |
43 | distance[i][0] = i; | |
44 | for (int j = 1; j <= rhs.length(); j++) | |
45 | distance[0][j] = j; | |
46 | ||
47 | for (int i = 1; i <= lhs.length(); i++) | |
48 | for (int j = 1; j <= rhs.length(); j++) | |
49 | distance[i][j] = minimum( | |
50 | distance[i - 1][j] + 1, | |
51 | distance[i][j - 1] + 1, | |
52 | distance[i - 1][j - 1] + ((lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1)); | |
53 | ||
54 | return distance[lhs.length()][rhs.length()]; | |
55 | } | |
56 | } |