A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 邓熊财 中级黑马   /  2015-5-22 00:49  /  472 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package stack1;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Test3 {
        public static void main(String [] args){
                TreeMap<String,ArrayList<Student>> tm = new TreeMap<String,ArrayList<Student>>(
                                new Comparator<String>(){

                                        @Override
                                        public int compare(String s1, String s2) {
                                               
                                                //这里相减正常(下面ArrayList可以正常)
                                                //int num =  s1.hashCode()-s2.hashCode();
                                                //相加空指针异常
                                                //return num;
                                                //正数或者负数空指针异常
                                                //return -1;
                                                //为0是遍历方式1只有一半打印的出来
                                                return -1;
                                        }
                                       
                                });
                String s = new String("110");
                String s1 = new String("111");
               
                ArrayList<Student> ar = new ArrayList<Student>();
                ar.add(new Student("赵薇",22));
                ar.add(new Student("赵",2));
                ar.add(new Student("赵薇",2));
                ar.add(new Student("赵薇",20));
                tm.put(s, ar);
               
                ArrayList<Student> a = new ArrayList<Student>();
                a.add(new Student("赵0",22200));
                a.add(new Student("赵薇",22));
                a.add(new Student("赵薇",222));
                a.add(new Student("赵薇",223));
                tm.put(s1,a);
               
                //可以打印出
        /*        System.out.println(tm);
         * 打印为null
                System.out.println(tm.get(s));
                //打印正常
                System.out.println(tm);*/
//                空指针异常
                Set<String> set = tm.keySet();
                System.out.println(set.size());
                //for(String s : set){
                        //System.out.println(s+tm.get(s));
                        //ArrayList<Student> at = tm.get(s);
                        /*for(Student am : at){
                                System.out.println(am.getName()+" "+am.getAge());*/
                       
                //}
                //正常情况
        /*        Set<Map.Entry<String, ArrayList<Student>>> set = tm.entrySet();
                for (Map.Entry<String, ArrayList<Student>> entry : set) {
                        //System.out.println(entry);
                        System.out.println(entry.getKey()+":");
                        ArrayList<Student> al = entry.getValue();
                        for (Student student : al) {
                                System.out.println(student.getName()+"***"+student.getAge());
                        }
                }*/
                标准的学生类不上传了
        }
}

2 个回复

倒序浏览
注解上面的两个put中原来是直接传的int的数字当然是用双引号引起来的哦!
回复 使用道具 举报
完整的写一下吧。1.需求是什么;2.完整代码贴一下。你这样贴出来,大家看起来会很累,会有抵触情绪和浪费时间。:)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马