第1个回答 2020-03-03
原来写的一个练习,和这个异曲同工。自己看看吧
/*
编写一个方法,返回一个double类型的数组;
数组中的元素通过解析字符串中的元素获得;
如:
"1,2;3,4,5;6,7,8"
对应的数组为:
d[0,0]=1.0
d[0,1]=2.0
d[1,0]=3.0
d[1,1]=4.0
d[1,2]=5.0
d[2,0]=6.0
d[2,1]=7.0
d[2,2]=8.0
*/
import
java.util.*;
public
class
splitstring
{
public
static
void
main(string[]
args)
{
system.out.println("please
input
your
string!");
scanner
sc
=
new
scanner(system.in);
string
str
=
sc.nextline();
system.out.println();
string[]
sfirst
=
str.split(";");
double[][]
d;
d
=
new
double[sfirst.length][];
for(int
i=0;i<sfirst.length;i++)
{
string[]
ssecond
=
sfirst[i].split(",");
d[i]
=
new
double[ssecond.length];
for(int
j=0;j<ssecond.length;j++)
{
d[i][j]
=
double.parsedouble(ssecond[j]);
}
}
for(int
i=0;
i<d.length;i++)
{
for(int
j=0;j<d[i].length;
j++)
{
system.out.print(d[i][j]+"
");
}
system.out.println();
}
}
}