62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.Comparator;
import java.util.TreeMap;
public class Demo {
public String[] foo(String[][] s){
TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int r = o1.compareTo(o2);
return r; // 反序就 reuturn -r;
}
});
for (String[] each : s) {
String[] tmp = each[0].split("\\s+");
map.put(Integer.parseInt(tmp[1]), each[0]);
}
return map.values().toArray(new String[]{});
}
public static void main(String[] args) {
String[][] s = {
{"s 2 aaa"},
{"raa 13 bbb"},
{"s 0 ccc"},
};
String[] out = new Demo().foo(s);
for (String each : out) {
System.out.println(each);
}
}
}
[/quote]
想问一下foo这个函数返回的是一个二维数组吗?还有如果想要得到排完续后的二维数组,就是调用foo这个函数咯?
import java.util.Comparator;
import java.util.TreeMap;
public class Demo {
public String[] foo(String[][] s){
TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int r = o1.compareTo(o2);
return r; // 反序就 reuturn -r;
}
});
for (String[] each : s) {
String[] tmp = each[0].split("\\s+");
map.put(Integer.parseInt(tmp[1]), each[0]);
}
return map.values().toArray(new String[]{});
}
public String[][] foo2(String[][] s){
TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int r = o1.compareTo(o2);
return r; // 反序就 reuturn -r;
}
});
for (String[] each : s) {
String[] tmp = each[0].split("\\s+");
map.put(Integer.parseInt(tmp[1]), each[0]);
}
String[] d1 = map.values().toArray(new String[]{});
String[][] d2 = new String[d1.length][1];
for(int i=0;i<d2.length;i++){
for(int j=0;j<d2[i].length;j++){
d2[i][j] = d1[i];
}
}
return d2;
}
public static void main(String[] args) {
String[][] s = {
{"s 2 aaa"},
{"raa 13 bbb"},
{"s 0 ccc"},
};
Demo d = new Demo();
String[][] a = d.foo2(s); // foo2返回2维数组
for (String[] strings : a) {
for (String string : strings) {
System.out.println(string);
}
}
}
}
[/quote]
想问一下二维数组里面
for (String[] each : s) {
String[] tmp = each[0].split("\\s+");
map.put(Integer.parseInt(tmp[1]), each[0]);
}
里面的each[0]表示的是什么意思呢?是s里面每一行的第一个元素吗?
import java.util.Comparator;
import java.util.TreeMap;
public class Demo {
public String[] foo(String[][] s){
TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int r = o1.compareTo(o2);
return r; // 反序就 reuturn -r;
}
});
for (String[] each : s) {
String[] tmp = each[0].split("\\s+");
map.put(Integer.parseInt(tmp[1]), each[0]);
}
return map.values().toArray(new String[]{});
}
public String[][] foo2(String[][] s){
TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int r = o1.compareTo(o2);
return r; // 反序就 reuturn -r;
}
});
for (String[] each : s) {
String[] tmp = each[0].split("\\s+");
map.put(Integer.parseInt(tmp[1]), each[0]);
}
String[] d1 = map.values().toArray(new String[]{});
String[][] d2 = new String[d1.length][1];
for(int i=0;i<d2.length;i++){
for(int j=0;j<d2[i].length;j++){
d2[i][j] = d1[i];
}
}
return d2;
}
public static void main(String[] args) {
String[][] s = {
{"s 2 aaa"},
{"raa 13 bbb"},
{"s 0 ccc"},
};
Demo d = new Demo();
String[][] a = d.foo2(s); // foo2返回2维数组
for (String[] strings : a) {
for (String string : strings) {
System.out.println(string);
}
}
}
}
String[][] num={{"s", "2", "aaa"},{"r", "1" ,"bbb"},{"s" ,"0", "ccc"},};
Map<String, String[]> m = new TreeMap<String, String[]>();
for (int i = 0; i < num.length; i++) {
m.put(Arrays.toString(num[i]).replaceFirst(".*?,\\s?(\\d+)\\s?,.*","$1"), num[i]);
}
for(Map.Entry<String, String[]> entry : m.entrySet())
{
System.out.println(entry.getKey()+": "+Arrays.toString(entry.getValue()));
}
String[][] num={{"s", "2", "aaa"},{"r", "1" ,"bbb"},{"s" ,"0", "ccc"},};
String[] temp = new String[3];
for (int i = 0; i < num.length; i++) {
temp[i]=Arrays.toString(num[i]).replaceFirst(".*?,\\s?(\\d+)\\s?,.*","$1");
}
Arrays.sort(temp);
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num[i].length; j++) {
if(j==1){
System.out.print(temp[i]+" ");;
}else{
System.out.print(num[i][j]+" ");
}
}
System.out.println();
}
import java.util.Comparator;
import java.util.TreeMap;
public class Demo {
public String[] foo(String[][] s){
TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int r = o1.compareTo(o2);
return r; // 反序就 reuturn -r;
}
});
for (String[] each : s) {
String[] tmp = each[0].split("\\s+");
map.put(Integer.parseInt(tmp[1]), each[0]);
}
return map.values().toArray(new String[]{});
}
public static void main(String[] args) {
String[][] s = {
{"s 2 aaa"},
{"raa 13 bbb"},
{"s 0 ccc"},
};
String[] out = new Demo().foo(s);
for (String each : out) {
System.out.println(each);
}
}
}
String[][] num={{"s", "2", "aaa"},{"r", "1" ,"bbb"},{"s" ,"0", "ccc"}};
String[] temp = new String[3];
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num[i].length; j++) {
if (j == 1) {
temp[i] = num[i][j];
}
}
}
Arrays.sort(temp);
for (int j = 0; j < num[1].length; j++) {
num[j][1]=temp[j];
}
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num[i].length; j++) {
System.out.print(num[i][j]+" ");
}
System.out.println();
}
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StringTest {
static String[][] sample = {{"s", "2", "aaa"}, {"r", "1", "bbb"}, {"s", "0", "ccc"}};
public static void main(String[] args) {
print(sample);
sort(sample, 1);
print(sample);
}
/**
* <pre>
* 排序
*
* date: 2014年7月11日
* </pre>
* @author hedley
* @param s 目标排序二维String数组
* @param columnIndex 以第几列为排序对象
*/
static void sort(String[][] s, final int columnIndex) {
List<String[]> helperList = Arrays.asList(s);
Collections.sort(helperList, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[columnIndex].compareTo(o2[columnIndex]);
}
});
s = (String[][])helperList.toArray();
}
static void print(String[][] s) {
for (String[] s1 : s) {
for (String s2 : s1) {
System.out.print(s2 + " ");
}
System.out.println();
}
}
}
String[][] arr={{"s", "2", "aaa"},{"r", "1" ,"bbb"},{"s" ,"0", "ccc"}};
Arrays.sort(arr, new Comparator<String[]>(){
@Override
public int compare(String[] o1, String[] o2) {
return o1[1].compareTo(o2[1]);
}
});
for(String[] s:arr){
System.out.println(Arrays.toString(s));
}