10黑马币
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- //“1.2,3.4,5.6,7.8,5.56,44.55”,
- public class test45 {
- public static void main(String[] args){
- String s = "1.2,3.4,5.6,7.8,5.56,44.55";
- Map<Integer,Integer> m = new TreeMap<Integer,Integer>();
- String[] strs = s.split(",");
- for(int x =0;x<strs.length;x++){
- String[] s1 = strs[x].split(".");
- m.put(Integer.parseInt(s1[0]), Integer.parseInt(s1[1]));
- }
- Set<Integer> set = m.keySet();
- Iterator<Integer> it = set.iterator();
- while(it.hasNext()){
- System.out.println("key="+it.next());
- }
- Collection<Integer> c = m.values();
- Iterator<Integer> itr = c.iterator();
- while(itr.hasNext()){
- System.out.println("value="+itr.next());
- }
- }
- }
复制代码
需求是将小数点两边的数分别作为键值存入map集合,但
String[] s1 = strs[x].split(".");
m.put(Integer.parseInt(s1[0]), Integer.parseInt(s1[1]))
这2行代码抛出ArrayIndexOutOfBoundsException,这是为什么啊?
|
最佳答案
查看完整内容
public class SDASDA {
public static void main(String[] args){
String s = "1.2,3.4,5.6,7.8,5.56,44.55";
Map m = new TreeMap();
String[] strs = s.split(",");
for(int x =0;x
|