Java在main方法中定义一个字符串数组abcdefghi,然后再method方法中将它们拼接成一个字符串,并将字符串长度及拼接的字符串打印,代码该怎么写?求大神救救我这个小白吧
第1个回答 2020-03-15
1、代码如下
public static void main(String[] args) {
String str[]={"a","b","c","d","e","f","g","h","i"};
System.out.println("字符串数组长度:" + str.length);
System.out.println("拼接后的字符串长度:" + method(str).length());
System.out.println("拼接后的字符串:"+method(str));
}
public static String method(String [] str){
String rs="";
for (String s : str) {
rs+=s;
}
return rs;
}
2、效果如图
第2个回答 2020-03-15
public class Test {
public static void main(String[] args){
String[] str={"a","b","c","d","e","f","g","h","i"};//定义字符串数组
method(str);
}
private static void method(String[] s){
if(s==null)
return;
StringBuffer sBuffer=new StringBuffer();
for(int i=0;i<s.length;i++){//将数组拼接成字符串
sBuffer.append(s[i]);
}
System.out.println("字符串长度:"+sBuffer.length());
System.out.println("拼接的字符串:"+sBuffer.toString());
}
}本回答被网友采纳
public static void main(String[] args){
String[] str={"a","b","c","d","e","f","g","h","i"};//定义字符串数组
method(str);
}
private static void method(String[] s){
if(s==null)
return;
StringBuffer sBuffer=new StringBuffer();
for(int i=0;i<s.length;i++){//将数组拼接成字符串
sBuffer.append(s[i]);
}
System.out.println("字符串长度:"+sBuffer.length());
System.out.println("拼接的字符串:"+sBuffer.toString());
}
}本回答被网友采纳