在Java中将String转换为数组2 D.

时间:2014-12-26 16:54:39

标签: java arrays string

我有这个String是查询的结果 07:40,09:00,10:20,11:40,| 08:00,09:00,|

1)我想消除最后一个,

2)将其转换为

String[][] matrix ={
    {"07:40","08:00"},
    {"09:00","09:00"},
    {"10:20",null},
    {"11:40",null}
};

4 个答案:

答案 0 :(得分:3)

我会:

1)消除最后一个","使用例如substring()

2)用string.split("|")分割字符串,并将长度保持为numTimes

3)循环分割结果并将每个子字符串拆分为substr.split(",")

4)将分组长度的最大长度保持在名为len

的int中

5)创建结果数组String[][] matrix = new String[len][numTimes]

5)创建一个for循环for (int i = 0; i < len; i++){...

6)在循环中将正确的值添加到matrix(检查为空)

答案 1 :(得分:1)

如果您使用的是C ++,那么可以试试这个:

#include <bits/stdc++.h>
using namespace std;

vector<string>split(string str,string Separator)
{
    vector<string>answer;string temp;
    int len=str.size();
    for(int i=0;i<len;i++)
    {
        bool isSeparator=false;
        for(int j=0;j<Separator.length();j++)
          if(str[i]==Separator[j])
            isSeparator=true;
        if(!isSeparator)
        {
            temp+=str[i];continue;
        }
        if(temp!="")
          answer.push_back(temp);temp="";
    }
    if(temp!="")
      answer.push_back(temp);
    return answer;
}
int main()
{
    int i,j;
    string str="07:40,09:00,10:20,11:40,|08:00,09:00,|";
    vector<string>v=split(str,"|"); // First split with respect to '|'
    vector<string>matrix[100]; // Maximum row of time
    for(i=0;i<v.size();i++)
    {
        vector<string>temp;
        temp=split(v[i],","); // Now split with respect to ','
        for(j=0;j<temp.size();j++)
        {
            matrix[j].push_back(temp[j]);
        }
    }
    for(i=0;;i++)
    {
        if(matrix[i].size()==0) // Break the loop, because no time will be on below
          break;
        for(j=0;j<matrix[i].size();j++)
          cout<<matrix[i][j]<<" ";
        cout<<"\n";
    }
return 0;
}

答案 2 :(得分:1)

试试这个:

public static void main(String ars[]) {
String string = "11:40,|08:00,09:00,|";
String[] str1 = string.split("\\|");
if (str1.length != 2) {
  throw new IllegalArgumentException("I dont see a seperator | in your String");
}
String[] rows = str1[0].split(",");
String[] cols = str1[1].split(",");
int maxLength = rows.length > cols.length ? rows.length : cols.length;
String matrix[][] = new String[maxLength][2];

for (int row=0; row<rows.length; row++) {
  matrix[row][0] = rows[row];
}
for (int col=0; col<cols.length; col++) {
  matrix[col][1] = cols[col];
}

for (int i=0; i<maxLength; i++) {
  System.out.println(Arrays.toString(matrix[i]));
}
}

答案 3 :(得分:0)

嗨试试这个解决方案它的工作原理,不仅是完美的方式,而且它处理空字符串和错误的字符串享受

             public static String[][] getModel(String answer){
     try {
         System.out.println(answer);
             if(answer.contains("|")){
                            String[] cols=answer.split(",\\|");

                            System.out.println("case found");
                            System.out.println("size 1:"+cols.length);

                            int dimensionCol=cols.length;
                            int dimensionRow=cols[0].split(",").length;

                            System.out.println(dimensionCol+" "+dimensionRow);
                            String[][] result=new String[dimensionRow][dimensionCol];

                            int i=0,j=0;
                            for(String colElement:cols){
                                i=0;
                                String[] datas = colElement.split(",");
                                for (String data : datas) {
                                    result[i][j]=(!data.equals(""))?data:null;
                                    System.out.print(result[i][j]);
                                    i++;
                                }
                                System.out.println("");
                                j++;
                                         }
                            return result;
                           }else{


                                  System.out.println("empty String return by null");
                                  return null;


                           }
          }catch(Exception e){e.printStackTrace();}
     return null;
   }

这是主要的测试方法

   public static void main(String[] args) {


   // String model = "07:40,09:00,10:20,11:40,|08:00,09:00,|";
    String model = "";
    String[][] model1 = getModel(model);
          if (model1!=null) {
        for (String[] model11 : model1) {
              for (String model111 : model11) {
                  System.out.print(model111+" ");
              }
              System.out.println("");

    }
    }

}