Java - 将字符串输入到数组中

时间:2014-11-13 02:29:23

标签: java arrays string-aggregation

我有一个这样的文本文件:

  

testcaseZ:a,b,c

     

testcaseX:b,d

     

testcaseC:b

我想要一个字符串数组,如:

         abcd
     

testcaseZ 1 1 1 0

     

testcaseX 0 1 0 1

     

testcaseC 0 1 0 0

1 个答案:

答案 0 :(得分:0)

public class testString {
    public static void main(String[] args){

        //need to configure this accordingly            
        int row=3+1;
        int col=4+1;

        String[][] array=new String[row][col];
        //construct the string with the number of row-1
        String s1="testcaseZ : a,b,c";
        String s2="testcaseX : b,d";
        String s3="testcaseC : b";
        String[] strings=new String[]{s1,s2,s3};//strings should have lenghth of row-1
        String[] allValues=new String[]{"a","b","c","d"};

        //polulate the first row
        array[0][1]="a";
        array[0][2]="b";
        array[0][3]="c";
        array[0][4]="d";
        //Above, you can  dynamically construct the two arrays based on your input(say from files)

        for(int i=1;i<array.length;i++){//for every rest rows
            String text=strings[i-1];
            String theCase=text.substring(0,text.indexOf(":")).trim();//
            String valueText=text.replaceAll(theCase, "").replaceFirst(":", "").trim();         
            array[i][0]=theCase;

            String[] values=valueText.split(",");
            List valueList=Arrays.asList(values);

            for(int j=1;j<array[i].length;j++){//for every cols
                if(valueList.contains(allValues[j-1])){//if contains the value, 
                    array[i][j]="1";
                }
                else array[i][j]="0";
            }
        }

        //test the output           
        System.out.println(Arrays.deepToString(array));
    }
}

输出如下:

[[null, a, b, c, d], [testcaseZ, 1, 1, 1, 0], [testcaseX, 0, 1, 0, 1], [testcaseC, 0, 1, 0, 0]]