首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java字符串有关问题

2012-03-28 
java字符串问题假设有一组数据1 2 42 3 54 5 63 4 2怎么获取字符串使a[]{1,2,4,3}b[]{2,3,54,}c[]{4,5,

java字符串问题
假设有一组数据
1 2 4
2 3 5
4 5 6
3 4 2

怎么获取字符串使
a[]={1,2,4,3}
b[]={2,3,54,}
c[]={4,5,6,2}

求大神帮忙

[解决办法]
矩阵置换
[解决办法]
for example

Java code
String s[][] = {    {"1","2","4"},    {"2","3","5"},    {"4","5","6"},    {"3","4","2"}}String[][] sa = new String[s[0].length][s.length]; //矩阵行列转换for (int i=0; i<s.length;i++) {    for (int j=0; j<s[i].length; j++) {        sa[j][i] = s[i][j];    }}String[] a = sa[0];String[] b = sa[1];String[] c = sa[2];System.out.println(Arrays.toString(a));System.out.println(Arrays.toString(b));System.out.println(Arrays.toString(c));
[解决办法]
探讨
for example

Java code
String s[][] = {
{"1","2","4"},
{"2","3","5"},
{"4","5","6"},
{"3","4","2"}
}
String[][] sa = new String[s[0].length][s.length]; //矩阵行列转换
for (int i=0;……

[解决办法]
问问题也分批次,害人不浅啊
[解决办法]
for (String line = br.readLine(); line != null; line = br.readLine()) 

改成 for (String line = br.readLine(); line != null;)
[解决办法]
探讨
现在求二三列各种组合+次数
比如二三列: 输出:
1 101 (1 1 101和3 1 101) 所以输出 2
2 104 输出 2
3 103 输出 3
。。。。


[解决办法]
Java code
        List<String> list = new ArrayList<String>();        Map<String, Integer> map = new HashMap<String,Integer>();                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("e:/wcc.txt")));        for (String line = br.readLine(); line != null; line = br.readLine()) {            list.add(line.substring(2));        }        br.close();                for (String key : list) {            if (map.containsKey(key)) {                map.put(key, map.get(key) + 1);            } else {                map.put(key, 1);            }        }        for (String key : list) {            System.out.println(key + " " + map.get(key));        }
[解决办法]
wcc.txt的行数可变的话,更简单了,O(n)就可以了。

Java code
        Map<String, Integer> map = new TreeMap<String,Integer>();                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("e:/wcc.txt")));        for (String line = br.readLine(); line != null; line = br.readLine()) {            String key = line.substring(2);            if (map.containsKey(key)) {                map.put(key, map.get(key) + 1);            } else {                map.put(key, 1);            }        }        br.close();                for (String key : map.keySet()) {            System.out.println(key + " " + map.get(key));        }
[解决办法]
for example
Java code
import java.util.*;public class Test {    public static void main(String[] args) throws Throwable {        List<String[]> data = new ArrayList<String[]>();        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("wcc.txt")));           for (String line = br.readLine(); line != null; line = br.readLine()) {               if (! line.matches("\\d+(\\s+\\d+){2,}\\s*")) continue;            String[] s1 = line.split("\\s+");            boolean found = false;            for (String[] s2 : data) {                if (s1[1].equals(s2[0]) && s1[2].equals(s2[1])) {                    s2[2] = String.valueOf(Integer.valueOf(s2[2]) + 1);                    found = true;                    break;                }            }            if (! found) {                for (int i=0; i<s1.length-1; i++) {                    s1[i] = s1[i+1];                }                s1[s1.length-1] = "1";                data.add(s1);            }        }           br.close();        for (String[] sa : data) {            for (String s : sa) {                System.out.printf("%s ", s);            }            System.out.println();        }    }} 

热点排行