我有一个字符串,我需要分解成块并将每个块保存在一个数组中以进行操作。我不知道块的长度,但我知道在第8个逗号之后新的记录开始了。我能够将其分解为正确的块并将其存储在循环中的StringBuffer
中,但不确定如何将其添加到数组中?
下面的代码显示了我到目前为止所做的事情:
public static void main(String[] args) {
//I would like to store each of these lines in an array
String teachMod = ("1,TSM,501,SM1056,Simon Thorne,TCO200,24,30,"+
"2,TSM,500,AC1157,Peter Jones,TCO104,24,60,"+
"3,TSM,500,SM0156,Simon Thorne,TCO113,12,60,"+
"4,TSM,500,AC1157,Peter Jones,TCO104,24,30,"+
"5,TSM,500,AC1156,Richard Adlam,TCO113,12,60,"+
"6,TSM,500,AC1157,Peter Jones,TCO109,12,90,"+
"7,TSM,503,SM0156,Simon Thorne,CIS414,12,30,"+
"8,TSM,501,AC1156,Richard Adlam,TCO222,12,40,"+
"9,TSM,500,AC1156,Richard Adlam,TCO114,12,60,"+
"10,TSM,501,AC1157,Peter Jones,TCO200,60,40,"+
"11,TSM,500,AC1156,Richard Adlam,TCO106,24,60,"+
"12,TSM,501,SM0156,Simon Thorne,TCO207,12,30,"+
"13,TSM,500,AC1157,Peter Jones,TCO112,24,30,");
StringBuffer sb = new StringBuffer();
char[] c = teachMod.toCharArray();
int j = 0;
for(int i = 0; i < teachMod.length(); i++){
sb.append(c[i]);
if(c[i]==','){
j++;
if(j==8){
//At this point the variable sb holds what I want in an array
System.out.println(sb);
j=0;
sb = new StringBuffer();
}
}
}
}
}//End class
答案 0 :(得分:3)
你可以将它们存储在这样的数组中:
String[] teachMod = { "first line", "second line" /* other line*/ };
答案 1 :(得分:1)
喜欢这个
ArrayList<String[]> values = new ArrayList<String[]> ();
String[] split = teachMod.split(",");
for (int i = 0; i < split.length;) {
String[] row = new String[8];
for(int j=0;j<8;j++,i++){
row[j]= split[i];
}
values.add(row);
}
String[][] rows = values.toArray(new String[0][]);
打印数组
for (int i = 0; i < rows.length;i++) {
String s="";
for(int j=0;j<8;j++){
s=s+rows[i][j] + " ";
}
System.out.println(s);
}
输出
1 TSM 501 SM1056 Simon Thorne TCO200 24 30
2 TSM 500 AC1157 Peter Jones TCO104 24 60
3 TSM 500 SM0156 Simon Thorne TCO113 12 60
4 TSM 500 AC1157 Peter Jones TCO104 24 30
5 TSM 500 AC1156 Richard Adlam TCO113 12 60
6 TSM 500 AC1157 Peter Jones TCO109 12 90
7 TSM 503 SM0156 Simon Thorne CIS414 12 30
8 TSM 501 AC1156 Richard Adlam TCO222 12 40
9 TSM 500 AC1156 Richard Adlam TCO114 12 60
10 TSM 501 AC1157 Peter Jones TCO200 60 40
11 TSM 500 AC1156 Richard Adlam TCO106 24 60
12 TSM 501 SM0156 Simon Thorne TCO207 12 30
13 TSM 500 AC1157 Peter Jones TCO112 24 30
答案 2 :(得分:0)
只需声明一个数组
String[] arr = new String[100];
int count = -1;
并添加如下
if(j==8){
//At this point the variable sb holds what I want in an array
array[++count] = sb.toString();
System.out.println(sb);
j=0;
sb = new StringBuffer();
}
虽然我建议使用Arraylist而不是数组,因为那时你不需要预先确定大小。
ArrayList<String> al = new ArrayList<Str>();
添加
al.add(sb.toString());
答案 3 :(得分:0)
获取8个逗号后,您可以使用String[] arr=sb.toString().split(",")
if (j == 8) {
//At this point the variable sb holds what I want in an array
System.out.println(sb);
String[] arr=sb.toString().split(",");
for (String string : arr) {
System.out.println("Arr value :"+string);
}
j = 0;
sb = new StringBuffer();
}
答案 4 :(得分:0)
已经提出了一些很好的建议,但我正在寻找你的算法,它看起来有点......对我而言很长;)
所以我想出了这个,它使用的事实是你知道每个第8个逗号开始一个新记录。考虑到这一点,我能够将teachMod
String
拆分成一个数组,然后简单地逐步扫描每个记录。
我只是使用System#arraycopy
制作数组的新副本,并将其添加到ArrayList
。
现在,在这个烂摊子的某个地方,实际上可以计算预期记录的数量,但我懒得被打扰;)
import java.util.ArrayList;
import java.util.List;
public class BreakingString {
public static void main(String[] args) {
String teachMod = ("1,TSM,501,SM1056,Simon Thorne,TCO200,24,30,"
+ "2,TSM,500,AC1157,Peter Jones,TCO104,24,60,"
+ "3,TSM,500,SM0156,Simon Thorne,TCO113,12,60,"
+ "4,TSM,500,AC1157,Peter Jones,TCO104,24,30,"
+ "5,TSM,500,AC1156,Richard Adlam,TCO113,12,60,"
+ "6,TSM,500,AC1157,Peter Jones,TCO109,12,90,"
+ "7,TSM,503,SM0156,Simon Thorne,CIS414,12,30,"
+ "8,TSM,501,AC1156,Richard Adlam,TCO222,12,40,"
+ "9,TSM,500,AC1156,Richard Adlam,TCO114,12,60,"
+ "10,TSM,501,AC1157,Peter Jones,TCO200,60,40,"
+ "11,TSM,500,AC1156,Richard Adlam,TCO106,24,60,"
+ "12,TSM,501,SM0156,Simon Thorne,TCO207,12,30,"
+ "13,TSM,500,AC1157,Peter Jones,TCO112,24,30,");
String parts[] = teachMod.split(",");
List<String[]> rows = new ArrayList<String[]>(25);
for (int index = 0; index < parts.length; index += 8) {
String[] record = new String[8];
System.arraycopy(parts, index, record, 0, 8);
rows.add(record);
}
String[] recordRows = rows.toArray(new String[rows.size()]);
}
}