import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* A utility class for Test 2.
*
* @author EECS2030
*
*/
public class Test2D {
private Test2D() {}
/**
* Returns the string at the end of the list t
*
* @param t
* a list
* @pre. t.size() > 0
* @return the string at the end of t
*/
public static String last(List<String> t) {
return t.get(t.size() - 1);
}
/**
* Returns the second last string in the list t
*
* @param t
* a list
* @return the second last string in t
* @throws IllegalArgumentException
* if the length of the list is less than 2
*/
public static String secondLast(List<String> t) {
if (t.size() < 2) {
throw new IllegalArgumentException("list must have length of 2");
}
return t.get(t.size() - 2);
}
/**
* Returns the string formed by concatenating all of the strings contained
* in the list t. The strings are concatenated sequentially starting from
* the first string in the list and ending with the last string in the list.
* For example, if t is the list:
*
* <p>
* ["he", "ll", "o"]
*
* <p>
* then the resulting string is "hello"
*
* @param t
* a list of strings to concatenate
* @return the concatenation of the strings in t
*/
public static String concat(List<String> t) {
StringBuilder b = new StringBuilder();
for (String s : t) {
b.append(s);
}
return b.toString();
}
/**
* Sorts a list of strings by their length (shortest first). Strings with
* the same length are sorted in dictionary order. For example, if the list
* t is:
*
* ["abstract", "boolean", "char", "code", "for"]
*
* then t is modified so that it becomes:
*
* ["for", "char", "code", "boolean", "abstract"]
*
* @param t
* the list to sort
*/
public static void sortByLength(List<String> t) {
Map<Integer, List<String>> lengths = new TreeMap<Integer, List<String>>();
for (String s : t) {
int sLen = s.length();
List<String> u = lengths.get(sLen);
if (u == null) {
u = new ArrayList<String>();
lengths.put(sLen, u);
}
u.add(s);
}
t.clear();
for (List<String> u : lengths.values()) {
Collections.sort(u);
t.addAll(u);
}
}
}