1

我有一个字符串,我需要将其分解为多个块并将每个块保存在一个数组中以进行操作。我不知道块的长度,但我知道在第 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
4

6 回答 6

2
//initialise array list which can store any length of chunks
ArrayList<String> chunks = new ArrayList<String>();
    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
                    //add chunk to chunk list
                    chunks.add(sb.toString());
                    System.out.println(sb);
                    j=0;
                    sb = new StringBuffer();


                }
            }
        }
    //convert chunk list to array
    String[] chunksAsArray = chunks.toArray(new String[chunks.size()]);
于 2013-11-12T06:46:09.940 回答
2

您可以将它们存储在这样的数组中:

String[] teachMod = { "first line", "second line" /* other line*/ };
于 2013-11-12T06:40:03.240 回答
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 
于 2013-11-12T06:47:59.917 回答
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()]);
    }

}
于 2013-11-12T06:57:07.217 回答
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();
  }
于 2013-11-12T06:42:30.030 回答
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());
于 2013-11-12T06:38:52.180 回答