简体   繁体   English

从字符串数组拆分为数组

[英]split into arrays from a string array

I am using Java 1.4 for an old project. 我正在为旧项目使用Java 1.4。

I have a string array 我有一个字符串数组

       String a[0]={"200,300"}
       String a[1]={"700,500"}
       String a[2]={"900,400"}  

as so on, 尽快,

for this I need to create 2 string array to get the first value in a string array and second value in different string array. 为此,我需要创建2个字符串数组以获取字符串数组中的第一个值,并获取不同字符串数组中的第二个值。

ex: 例如:

String a[0]="200"
String a[1]="700"
String a[2]="900"

String b[0]="300"
String b[1]="500"
String b[2]="400"

I am not getting how can I do this. 我不知道该怎么做。

If the length of the string array is known, 如果字符串数组的长度已知,

String[] newString = new String [6];
int count = 0;
for(int i = 0; i<3 ; i++){
   newString[i] = a[i];
   count++;
}
for(int j=0; j<3; j++){
   newString[count] = b[j];
   count++;
}

EDIT: Previously i got your question wrong. 编辑:以前我弄错了你的问题。 The following solution will help you. 以下解决方案将为您提供帮助。 to divide a into 2 string arrays. a分为2个字符串数组。

    String a[] = new String [3];
       a[0]={"200,300"};
       a[1]={"700,500"};
       a[2]={"900,400"};

    String[] newStr1 = new String [a.length];
    String[] newStr2 = new String [a.length];

    for(int i = 0; i<3 ; i++){
       String [] x= a[i].split(",");
       newStr1[i] = x[0];
       newStr2[i] = x[1];

    }

Looking at your code, this provides you the correct solution, 查看您的代码,这为您提供了正确的解决方案,

int n = 3; //This can change as your array size is not known
String a[] = new String [n];
   a[0]="200,300";
   a[1]="700,500";
   a[2]="900,400"; //And So on


String[] b1 = new String [n];
String[] b2 = new String [n];

for(int i = 0; i<n ; i++)
{
   String [] numbers= a[i].split(",");
   b1[i] = numbers[0];
   b2[i] = numbers[1];
}

First you cant defined String array as string a[1]={700,500} . 首先,您不能将String数组定义为string a[1]={700,500} It is wrong way. 这是错误的方式。

And second, Here is what you want : 其次,这是您想要的:

a[0]=new String[]{"200","300"};
a[1]=new String[]{"700","500"};
a[2]=new String[]{"900","400"};

String x[] = new String[a.length];
String y[] = new String[a.length];
for(int i=0;i<a.length;i++){
    x[i] = a[i][0];
    y[i] = a[i][1];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM