Arrays 从字符数组打印字符串?

Arrays 从字符数组打印字符串?,arrays,string,char,Arrays,String,Char,当前,代码应将字符串中的所有空格替换为%20。在大多数情况下,我认为我调用的方法的逻辑是正确的,但是当我想看到返回的结果时,我的主方法什么也不打印——它为字符串打印一个空白。有人能告诉我把数组转换成字符串的逻辑哪里不正确吗?谢谢你的时间和帮助。我附上了我的方法代码,以防你们需要它。对不起,如果这个错误是愚蠢的 public class replaceSpaces { public static void main(String[] args) { char[] data = checkf

当前,代码应将字符串中的所有空格替换为%20。在大多数情况下,我认为我调用的方法的逻辑是正确的,但是当我想看到返回的结果时,我的主方法什么也不打印——它为字符串打印一个空白。有人能告诉我把数组转换成字符串的逻辑哪里不正确吗?谢谢你的时间和帮助。我附上了我的方法代码,以防你们需要它。对不起,如果这个错误是愚蠢的

public class replaceSpaces {
public static void main(String[] args) {
    char[] data = checkfor20("yo o");
    String text = String.valueOf(data); //turn char array into string representation 
    System.out.println(" new one: " + text); 
}

private static char[] checkfor20(String string) {
    int check = string.length(); //length of string 
    int spaceCount = 0, newLength;  //count spaces and new length with %20 put in 
    char[] charstring = string.toCharArray(); // turn string into char array 
    for(int i = 0; i < check ; i++) { //get space count for newlenght
        if (charstring[i] == ' ') {
            spaceCount++; 
    }
}
    newLength = check + (spaceCount * 2);
    char[] newArray = new char[newLength]; 
    for(int i = check - 1; i >= 0; i--) {
        if(newArray[i] == ' ') { //get spaces and put it
            newArray[newLength - 1 ] = '0'; 
            newArray[newLength - 2 ] = '2'; 
            newArray[newLength - 3 ] = '%';
            newLength = newLength - 3; 
        }
        else { 
            newArray[newLength - 1] = charstring[i]; 
            newLength = newLength -  1; 

        }

    }
    return newArray; //return the new char array 

}}
公共类替换空间{
公共静态void main(字符串[]args){
char[]data=checkfor20(“yoo”);
String text=String.valueOf(data);//将字符数组转换为字符串表示
System.out.println(“新的:+文本);
}
私有静态字符[]checkfor20(字符串){
int check=string.length();//字符串的长度
int spaceCount=0,newLength;//计算空间和新长度,并放入%20
char[]charstring=string.toCharArray();//将字符串转换为字符数组
对于(int i=0;i=0;i--){
if(newArray[i]=''){//获取空格并将其放入
newArray[newLength-1]=“0”;
newArray[newLength-2]=“2”;
newArray[newLength-3]='%';
newLength=newLength-3;
}
否则{
newArray[newLength-1]=字符串[i];
newLength=newLength-1;
}
}
return newArray;//返回新的字符数组
}}

为什么不
newstring=oldstring.replace(“,“%20”)

为我本应将数组更改为的错误道歉

if(charstring[i] == ' ')
而不是我创建的新数组