我有一个char数组:

char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};

我目前的解决方案是做

String b = new String(a);

但肯定有更好的办法吧?


当前回答

package naresh.java;

public class TestDoubleString {

    public static void main(String args[]){
        String str="abbcccddef";    
        char charArray[]=str.toCharArray();
        int len=charArray.length;

        for(int i=0;i<len;i++){
            //if i th one and i+1 th character are same then update the charArray
            try{
                if(charArray[i]==charArray[i+1]){
                    charArray[i]='0';                   
                }}
                catch(Exception e){
                    System.out.println("Exception");
                }
        }//finally printing final character string
        for(int k=0;k<charArray.length;k++){
            if(charArray[k]!='0'){
                System.out.println(charArray[k]);
            }       }
    }
}

其他回答

试试这个

Arrays.toString(array)

另一种方法是:

String b = a + "";

只使用字符串。以下值;

  private static void h() {

        String helloWorld = "helloWorld";
        System.out.println(helloWorld);

        char [] charArr = helloWorld.toCharArray();

        System.out.println(String.valueOf(charArr));
    }

你也可以使用StringBuilder类

String b = new StringBuilder(a).toString();

String或StringBuilder的使用因方法需求而异。

String str = "wwwwww3333dfevvv";
char[] c = str.toCharArray();

现在要将字符数组转换为String,有两种方法。

Arrays.toString(c);

返回字符串[w, w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v]。

And:

String.valueOf(c)

返回字符串wwwwww3333devvv。

总结:注意Arrays.toString(c),因为你会得到“[w, w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v]”而不是“wwwwww3333dfevvv”。