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
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));
[解决办法]
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)就可以了。
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
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(); } }}