解决方案 »
- 传值
- java中的枚举类型问题
- web.xml问题!!!
- 请教高手,请问在java中调用ocx或者com组件,如何收到它们的事件以及传出参数?
- 各位兄弟姐妹,谁用java做过图象获取及处理,请进来看看,有分相送的哦!
- 初学者问:用浏览器运行helloworld程序,为什么是空的?状态栏提示:load:class HelloWorldApplet not found ,为什么会找不到呢?
- 这么多问题,伤人啊!
- jdk1.4.0在win2k下如何配置才仍正常使用
- 如何将.mdb数据库连接到JDBC?
- Java提供父类无法使用
- java swing组件使用过程中出现这样的问题怎么办啊
- 新人 求助。有关JAVA 类和方法的问题
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));
}
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[] 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();
}
和我想的一样,增加了一点代码,处理可能字符串格式不标准,中间空格很多的自动分割处理。
比如:
String[][] s = {
{"s 2 aaa"},
{"raa 13 bbb"},
{"s 0 ccc"},
};增加正序,反序排列的逻辑。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);
}
}
}
for (int j = 0; j < num[1].length; j++) {
num[j][1]=temp[j];
}
还有这样是根据数字的大小来排序的吗?因为这些数字都是string类型的。。
那个for是重新赋值第2 列。
也可以
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();
}
s 2 aaa s 0 ccc
r 1 bbb 到 r 1 bbb
s 0 ccc s 2 aaa
就是整行数据都要移动。。
s 2 aaa s 0 ccc
r 1 bbb 到 r 1 bbb
s 0 ccc s 2 aaa
就是整行数据都要移动。。
我做错了。不对
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[][] s = {
{"s 2 aaa"},
{"raa 13 bbb"},
{"s 0 ccc"},
};增加正序,反序排列的逻辑。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);
}
}
}
想问一下foo这个函数返回的是一个二维数组吗?还有如果想要得到排完续后的二维数组,就是调用foo这个函数咯?
不是,前面的那个代码,foo是返回1维数组
要返回2维数组,新的代码中foo2,返回2维数组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);
}
}
}
}
不是,前面的那个代码,foo是返回1维数组
要返回2维数组,新的代码中foo2,返回2维数组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);
}
}
}
}
想问一下二维数组里面
for (String[] each : s) {
String[] tmp = each[0].split("\\s+");
map.put(Integer.parseInt(tmp[1]), each[0]);
}
里面的each[0]表示的是什么意思呢?是s里面每一行的第一个元素吗?
{"s 2 aaa"},
{"raa 13 bbb"},
{"s 0 ccc"},
};String[] each 是一个1维数组,其中第0个元素是数组{"s 2 aaa"},第1个元素是数组 {"raa 13 bbb"},第2个元素是数组{"s 0 ccc"}each[0]就是1维数组each的第0个元素,
当each是数组{"s 2 aaa"}的时候,each[0]就是"s 2 aaa"
当each是数组 {"raa 13 bbb"}的时候,each[0]就是"raa 13 bbb"假设each是数组{"hello world", "s 2 aaa"}的时候,each[0]就是"hello world"
String[] each是一个1维数组,其中的元素不是别的,是数组!
比如: {"s 2 aaa"} 这个是一个数组,1维,包含1个元素,元素类型是字符串 "s 2 aaa"!