java中,如何把一个字符串转换成数组?

如题:
String st="abcdefg“,如何转换成数组 string [] ary={a,b,c,d,e,f.g}.thanks in advance.

1.字符串转字符
for(int i = 0; i < str.length ; i++ )
  str.charAt(i);
2+3:不想循环的话 可以用一个List装字符,每次装之前调用if(List.contains(..))

   


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);
}

}

 或者

static Map sortMap(Map map) { 
     List list = new LinkedList(map.entrySet()); 
     Collections.sort(list, new Comparator() { 
     public int compare(Object o1, Object o2) { 
     int result = ((Comparable) ((Map.Entry) (o1)).getValue()) 
     .compareTo(((Map.Entry) (o2)).getValue());
     return result == 0?
      ((Comparable) ((Map.Entry) (o1)).getKey()) 
.compareTo(((Map.Entry) (o2)).getKey())
:result;             
     } 
     }); 
     Map result = new LinkedHashMap(); 
     for (Iterator it = list.iterator(); it.hasNext();) { 
     Map.Entry entry = (Map.Entry)it.next(); 
     result.put(entry.getKey(), entry.getValue()); 
     } 
     return result; 
    }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-01
public static void main(String[] args)
{
String str = "hello world!";//要转换的字符串

int len = str.length();//字符串长度
String strArray[] = new String[len];
//开始转换
for(int i = 0; i < len; i++)
strArray[i] = str.charAt(i) + "";

//查看结果
for(String s:strArray)
System.out.println(s);
}本回答被提问者采纳
第2个回答  2013-08-01
直接用st.toCharArray();返回一个char[]数组,非要String的话可以强转
第3个回答  2013-08-01
用字符串分割Sting[] ary=st.split("");
第4个回答  2013-08-01
st.toCharArray();
相似回答