Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中将字符串转换为数组2D_Java_Arrays_String - Fatal编程技术网

在java中将字符串转换为数组2D

在java中将字符串转换为数组2D,java,arrays,string,Java,Arrays,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} }; 如果你使用C++,那么你可以试试这个: #include <bits/stdc++.h> using namespa

我知道这个字符串是查询的结果 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}
};

如果你使用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;
}
#包括
使用名称空间std;
向量拆分(字符串str、字符串分隔符)
{
向量应答;字符串温度;
int len=str.size();

对于(int i=0;i)如果你使用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;
}
#包括
使用名称空间std;
向量拆分(字符串str、字符串分隔符)
{
向量应答;字符串温度;
int len=str.size();
对于(int i=0;ii),我会:

1) 使用例如
substring()

2) 使用
string拆分字符串。拆分(“|”)
,并将长度保持为
numTimes

3) 在拆分结果上循环并按
substr.split(“,”)

4) 将拆分长度的最大长度保留在名为
len

5) 创建结果数组
String[][]矩阵=新字符串[len][numTimes]

5) 为(int i=0;i

6) 在循环中,将正确的值添加到
矩阵中
(检查空值)

我会:

1) 使用例如
substring()

2) 使用
string拆分字符串。拆分(“|”)
,并将长度保持为
numTimes

3) 在拆分结果上循环并按
substr.split(“,”)

4) 将拆分长度的最大长度保留在名为
len

5) 创建结果数组
String[][]矩阵=新字符串[len][numTimes]

5) 为(int i=0;i

6) 在循环中,将正确的值添加到矩阵中(检查是否为空)

尝试以下操作:

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]));
}
}
publicstaticvoidmain(字符串ars[]){
String String=“11:40,| 08:00,09:00,|”;
String[]str1=String.split(“\\\\”);
如果(str1.length!=2){
抛出新的IllegalArgumentException(“我在你的字符串中没有看到分隔符”);
}
字符串[]行=str1[0]。拆分(“,”;
字符串[]cols=str1[1]。拆分(“,”;
int maxLength=rows.length>cols.length?rows.length:cols.length;
字符串矩阵[][]=新字符串[maxLength][2];
对于(int row=0;row请尝试以下操作:

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]));
}
}
publicstaticvoidmain(字符串ars[]){
String String=“11:40,| 08:00,09:00,|”;
String[]str1=String.split(“\\\\”);
如果(str1.length!=2){
抛出新的IllegalArgumentException(“我在你的字符串中没有看到分隔符”);
}
字符串[]行=str1[0]。拆分(“,”;
字符串[]cols=str1[1]。拆分(“,”;
int maxLength=rows.length>cols.length?rows.length:cols.length;
字符串矩阵[][]=新字符串[maxLength][2];

对于(int row=0;rowhi),请尝试此解决方案,它不仅可以正常工作,而且可以处理空字符串和格式错误的字符串

             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("");

    }
    }

}

大家好,尝试一下这个解决方案吧,它不仅可以很好地工作,而且可以处理空字符串和格式错误的字符串

             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("");

    }
    }

}


哪种编程语言?是固定格式,即用管道分隔的列,即“|”?否是查询结果中的一个变量,是的,我用管道分隔列是否总是6个变量?顺序是否重要?管道字符是否固定?这里没有足够的信息来帮助哪种编程语言?是固定格式,即用管道分隔列,即“|”?否是查询结果中的一个变量,是的,我用管道分隔列。它总是6个变量吗?顺序重要吗?管道字符是否固定?这里没有足够的信息来帮助您。谢谢,但OP需要java的答案。@almasshaikh最初没有说明:(我用的是坦克java@AliAkber我知道,我确实要求OP补充。非常感谢您帮助OP。谢谢,但OP需要java的答案。@almasshaikh一开始没有说明:(我用的是坦克java@AliAkber我知道,我确实要求OP添加。非常感谢你帮助OP。我不认为写出OP的所有代码是OP学习的好方法:/n不,我同意你的观点@CyberneticWerkGuruorc。但我希望他能熟悉JAVA中的API并学习它们,以便下次他会使用它们。但如果我有String String=“11:40,| 08:00,09:00,| 08:00,09:00”应该是dynamic@almasshaikh帮帮我,plzI不认为把OP的所有代码都写出来是OP学习的好方法:/不,我同意你的观点@CyberneticWerkGuruorc。但我希望他能熟悉JAVA中的API并学习它们,这样下次他就可以使用它们了。但是如果我有String=“11:40,| 08:00,09:00,| 08:00,09:00”应该是dynamic@almasshaikh请帮助我简洁地解释流程的必要步骤。请帮助我简洁地解释流程的必要步骤