當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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 Witer 
        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 Witer 
        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()



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 CharArrayWriter toCharArray() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。