JAVA中怎么把字符串转换成字符数组

如题所述

一、转换方法

String类提供了toCharArray()方法用于将字符串转换为字符串数组,其原型如下:

char[] java.lang.String.toCharArray()


二、测试程序

public class Main {
public static void main(String[] args) {
String source = "Hello,world!";
char[] arr = null;

arr = source.toCharArray();

for(int i=0; i<arr.length; i++) {
System.out.print(arr[i]);
}

}
}


三、运行测试:

Hello,world!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-09-09

第一种


String str ="sdsadw";

char[] cARR=cARR[str.length];

for(int i = 0; i < str.length ; i++ )
cARR[i]=str.charAt(i);



第二种

package com.xuz.csdn.worldcup.day22;
 
import java.util.HashMap;
import java.util.Map;
 
public class HelloWorldCountTest {
 
    public static void main(String[] args) {
        String hello = "helloworld!";
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        char[] ch = hello.toCharArray();
        for (char c : ch) {
            Integer i = map.get(c);
            if (i == null) {
                map.put(c, 1);
            } else {
                map.put(c, i.intValue() + 1);
            }
        }
         
        System.out.println(map);
    }   
 
}

本回答被提问者采纳
第2个回答  2016-09-09
不难。
获取到字符串的长度作为数组的长度,然后通过获取字符串某个位置的字符的方法,把获取到的字符存入数组里。用循环跑一下这个流程,就行了。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网