1 public class PairDemo
2 {
3 public static void main(String[] args)
4 {
5 String[] names = { "Tom", "Diana", "Harry" };
6 Pair<String, Integer> result = firstContaining(names, "a");
7 System.out.println(result.getFirst());
8 System.out.println("Expected: Diana");
9 System.out.println(result.getSecond());
10 System.out.println("Expected: 1");
11 }
12
13 /**
14 Gets the first String containing a given string, together
15 with its index.
16 @param strings an array of strings
17 @param sub a string
18 @return a pair (strings[i], i) where strings[i] is the first
19 strings[i] containing str, or a pair (null, -1) if there is no
20 match.
21 */
22 public static Pair<String, Integer> firstContaining(
23 String[] strings, String sub)
24 {
25 for (int i = 0; i < strings.length; i++)
26 {
27 if (strings[i].contains(sub))
28 {
29 return new Pair<String, Integer>(strings[i], i);
30 }
31 }
32 return new Pair<String, Integer>(null, -1);
33 }
34 }