String string1=new String("HelloWrold!");//定义了一个String类的对
                                            // 象并赋值:        Char[]  char1=new Char[20];//定义并初始化一个Char数组char1
我现在想把string1中的“HelloWorld"赋给char1,该怎样做?

解决方案 »

  1.   

    string1.getChars(0,string1.length(), char1, 0);
      

  2.   

    public class testStringToChar( ){  public static void main(String[] args){
      String string1=new String("HelloWorld!");
      Char[] char1=new Char[20];
      char1=string1;
         for(int i=0;i<string1.length;i++)
           {
            System.out.print(char1[i]);
        
           }
      
        }}
    编译出错:
    D:\MyJavaProject\testStringToChar.java:1: '{' expected
    public class testStringToChar( ){
                                 ^
    D:\MyJavaProject\testStringToChar.java:18: '}' expected
    }
     ^
    2 errors
    是何原因?是不是char1=string1;这条语句的错?
      

  3.   

    string1.toCharArray()就可以了
      

  4.   

    public class testStringToChar( ){
                                 ^
    有^符号的那个位上是不是用了中文全角??用英文半角就行了。
      

  5.   

    toCharArray
    public char[] toCharArray()
    Converts this string to a new character array. Returns:
    a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
    这些问题完全可以到api上找,何必浪费分数呢!养成自己动手的好习惯。