JLa*_*nce 1 java arrays string split
如何添加所有charAcro []来创建字符串?示例charAcro [0] = a,charAcro [1] = b,charAcro [2] = c,这使得字符串abc
while(resultSet.next()){
String Name = rs.getString(1);
String Acro=Name;
String delimiterAcro = " ";
String[] temp =null;
char[] charAcro = null;
temp = Name.split(delimiterAcro);
for(int i = 0;i<temp.length;++i){
charAcro[i] = temp[i].charAt(0);
//SOME CODE HERE?
}
}
Run Code Online (Sandbox Code Playgroud)
有一个String可用的构造函数,一个char数组,
char data[] = {'a', 'b', 'c'};
String str = new String(data);
Run Code Online (Sandbox Code Playgroud)
我正在研究它,请研究Java命名约定.类名应该是名词,大小写混合,每个内部单词的首字母大写.变量是大小写混合的小写首字母.内部单词以大写字母开头.
例如,在您的代码段中,
String Name = rs.getString(1);
Run Code Online (Sandbox Code Playgroud)
这不好,如果不是错的话.
而且,你可能在行上有一个NullPointerException,
charAcro[i] = temp[i].charAt(0);
Run Code Online (Sandbox Code Playgroud)
因为你没有初始化数组.以下是应该适合您的代码.
String name = rs.getString(1);
String[] temp = name.split(" ");
char[] charAcro = new char[temp.length];
for (int i = 0; i < temp.length; ++i) {
charAcro[i] = temp[i].charAt(0);
}
System.out.println(new String(charAcro));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7481 次 |
| 最近记录: |