Lets say I have 1d array
String teams[]={"Michael-Alex-Jessie","Shane-Bryan"};
and I want to convert it to multidimensional array which will be like this
String group[][]={{Michael,Alex,Jessie},{Shane,Bryan}}.
I try to use delimeter but for unknown reason I cannot assign the value of 1d to 2d array. It said incompatible types. Please any help will be much appreciated. Here is my code.
String [][]groups ;
String teams[]={"Michael-Alex-Jessie","Shane-Bryan"};
int a=0,b=0;
String del ="-/";
for (int count = 0; count < teams.length; count++)
{
groups[a][b] = teams[count].split(del);
a++;
}
Type of group[a][b]
is String
but you are attempting to assign string array (ie String[]
) there.
This is what you really want to do:
for (int count = 0; count < teams.length; count++) {
groups[count] = teams[count].split(del);
}
Yoy can't assign array of Strings returned by "teams[count].split(del)" to String
public class qq {
String[][] groups;
String teams[] = { "Michael-Alex-Jessie", "Shane-Bryan" };
int a = 0, b = 0;
public void foo() {
String del = "-/";
groups = new String[teams.length][];
for (int count = 0; count < teams.length; count++) {
groups[count] = teams[count].split(del);
a++;
}
}
}
您需要分配一个数组值,而不是一个单元格值:
groups[count] = teams[count].split(del);
String.split()返回字符串数组,而不是字符串
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.