public static String[][] convertToArrays (List list) {
Object[] obj = (Object[])list.get(0);
int objLength = obj.length;
String[][] str = new String[list.size()][objLength];
for (int i = 0; i < list.size(); i++) {
obj = (Object[])list.get(i);
for (int j = 0; j < objLength; j++) {
str[i][j] = obj[j].toString();
}
}
// Returns arrays
return str;
}
|
String dt = "09-28-80";
Date date = new SimpleDateFormat("MM-dd-yy").parse(dt);
String formattedDate = new SimpleDateFormat("yyyyMMdd").format(date);
|
* How would you sort this data by gender first and then by their last names?
*
* Jolie, Angelina, Female, 06/04/1975, Red
* Alba, Jessica, Female, 04/28/1981, Blue
* Einstein, Albert, 03/14/1879, Green
* Newton, Issac, 01/04/1643, Yellow
*
* This is how:
public static String[][] sortArrays (String[][] str) {
// Sorts by gender
Arrays.sort(str, new Comparator() {
public int compare(String[] o1, String[] o2) {
return o1[2].compareTo(o2[2]);
}
});
// Sorts by last name of first part of sorted gender
Arrays.sort(str, new Comparator() {
public int compare(String[] o1, String[] o2) {
if (o1[2].equals(o1[2])
return o1[0].compareTo(o2[0]);
else return 0;
}
});
// Sorts by last name of second part of sorted gender
Arrays.sort(str, new Comparator() {
public int compare(String[] o1, String[] o2) {
if (o1[2].equals(o2[2])
return o1[0].compareTo(o2[0]);
else return 0;
}
});
}
|
|