将一字符串放如二维数组中
讲一字符串如
s= "A0, A1, A2, A3, B0, B1, B2, B3, C0, C1, C2, C3, D0, D1, D2, D3, E0, E1, E2, E3, F0, F1, F2, F3, G0, G1, G2, G3, H0, H1, H2, H3, H4, H5, H6, H7, H8, H9, I0, J0, K0, L0, M0, N0, O0, O1, O2, O3, O4, O5, O6, P0, Q0, R0, S0, S1, S2, S3, S4, S5, S6, T0, U0, V0, W0 "
写一个方法返回一个数组存放以
array[a][0],array[a][1]....
array[b][0],array[b][1]....
这种存放方式
谢谢!!!
------解决方案--------------------为什么你需要二维数组呢?一维数组就可以了啊,你根据什么来要分为二维的呢?
------解决方案--------------------他的意思是
A0,A1,A2
array[A][0],a[A][1]...
------解决方案--------------------array[a][0],array[a][1]....
array[b][0],array[b][1]....
?????????????????????????????????
是不是array[0][0]= "A0 ",array[0][1]= "A1 ",array[0][2]= "A2 ",array[1][0]= "B0 ",array[1][1]= "B1 "这样的数组啊?
实在是没看懂
------解决方案--------------------说明白些,大家才可以帮助你哈。呵呵。
------解决方案--------------------下面的程序是不是lz要的,JDK1.5测试通过
import java.util.Arrays;
import java.util.List;
public class Test {
public String[][] toArray(String s){
String[] strArray = s.replace( " ", " ").split( "[,] ");
List <String> list = Arrays.asList(strArray);
String[][] array= new String[list.size()][2];
for(int i = 0; i < list.size(); i++)
{
String temp = list.get(i);
array[i][0] = temp.substring(0, 1);
array[i][1] = temp.substring(1);
}
return array;
}
public void printArray(String[][] array)
{
for(String[] temp1 : array){
for(String temp : temp1){
System.out.print(temp+ " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Test t = new Test();
String s = "A0, A1, A2, A3, B0, B1, B2, B3, C0, C1, C2, C3, D0, D1, D2, D3, E0, E1, E2, E3, F0, F1, F2, F3, G0, G1, G2, G3, H0, H1, H2, H3, H4, H5, H6, H7, H8, H9, I0, J0, K0, L0, M0, N0, O0, O1, O2, O3, O4, O5, O6, P0, Q0, R0, S0, S1, S2, S3, S4, S5, S6, T0, U0, V0, W0 ";
t.printArray(t.toArray(s));
}
}
=======================
以下为输出
A 0
A 1
A 2
A 3
B 0
B 1
B 2
B 3
C 0
C 1
C 2
C 3
D 0
D 1
D 2
D 3
E 0
E 1
E 2
E 3
F 0
F 1
F 2
F 3
G 0
G 1
G 2
G 3
H 0
H 1
H 2
H 3
H 4
H 5
H 6
H 7
H 8
H 9
I 0
J 0
K 0
L 0
M 0
N 0
O 0
O 1
O 2
O 3
O 4
O 5
O 6
P 0
Q 0
R 0
S 0
S 1
S 2
S 3
S 4
S 5
S 6
T 0
U 0
V 0
W 0