Java CharArrayWriter toCharArray()方法及示例

Java CharArrayWriter toCharArray()方法及示例

Java中 CharArrayWriter 类的 toCharArray() 方法返回输入数据的副本。

语法

public char[] toCharArray()

参数: 该方法不接受任何参数。

返回值: 该方法返回输入数据的副本。

以下程序说明上述方法:

程序1:

// Java program to illustrate
// the toCharArray() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing String Writer
        CharArrayWriter geek_writer1
            = new CharArrayWriter();
 
        char[] Sw = { 'G', 'E', 'E', 'K', 'S' };
 
        for (char c : Sw) {
 
            // Use of append(char Sw) :
            geek_writer1.append(c);
        }
 
        // Use of toCharArray() :
        char[] toChar1 = geek_writer1.toCharArray();
 
        for (char c1 : toChar1) {
            System.out.println("toCharArray : " + c1);
        }
    }
}

输出

toCharArray : G
toCharArray : E
toCharArray : E
toCharArray : K
toCharArray : S

程序2:

// Java program to illustrate
// the toCharArray() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing String Writer
        CharArrayWriter geek_writer1
            = new CharArrayWriter();
 
        char[] Sw = { 'G', 'O', 'P', 'A',
                      'L', 'D', 'A', 'V', 'E' };
 
        for (char c : Sw) {
            // Use of append(char Sw) :
            geek_writer1.append(c);
        }
 
        // Use of toCharArray() :
        char[] toChar1 = geek_writer1.toCharArray();
 
        for (char c1 : toChar1) {
            System.out.println("toCharArray : " + c1);
        }
    }
}

输出

toCharArray : G
toCharArray : O
toCharArray : P
toCharArray : A
toCharArray : L
toCharArray : D
toCharArray : A
toCharArray : V
toCharArray : E

参考资料 : https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#toCharArray()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南