cololection

package cn.bjsxt.col;
/**
 * 简化迭代器原理
 * hasNext
 * next
 * @author Administrator
 *
 */
public class MyArrayList {
    private String[] elem ={"a","b","c","d","e","f","g"};
    private int size = elem.length;
    
    
    private int cursor =-1;
    /**
     * 判断是否存在下一个元素
     * @return
     */
    public boolean hasNext(){        
        return cursor+1<size;
    }
    /**
     * 获取下一个元素
     */
    public String next(){
        cursor++; //移动一次
        return elem[cursor];
    }
    /**
     * 删除元素
     */
    public void remove(){
        
    }
    
    
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        MyArrayList list = new MyArrayList();
        while(list.hasNext()){
            System.out.println(list.next());
        }
        
        list = new MyArrayList();
        while(list.hasNext()){
            System.out.println(list.next());
        }
    }

}

 

 

 

package cn.bjsxt.col;

import java.util.Iterator;

/**
 * 简化迭代器原理 加入接口 提供方法
 * hasNext
 * next
 * @author Administrator
 *
 */
public class MyArrayList2 {
    private String[] elem ={"a","b","c","d","e","f","g"};
    private int size = elem.length;
    
    
    private class MyIt implements Iterator<String>{
        private int cursor =-1;
        /**
         * 判断是否存在下一个元素
         * @return
         */
        public boolean hasNext(){        
            return cursor+1<size;
        }
        /**
         * 获取下一个元素
         */
        public String next(){
            cursor++; //移动一次
            return elem[cursor];
        }
        /**
         * 删除元素
         */
        public void remove(){
            //没有实现
        }
    
    }
    
    public Iterator<String> iterator(){
        return new MyIt();
    }
    
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        MyArrayList2 list = new MyArrayList2();
        Iterator<String> it =list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
        
        it =list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
    }

}

 

 

package cn.bjsxt.col;

import java.util.Iterator;

/**
 * 简化迭代器原理 加入接口 提供方法
 * hasNext
 * next
 * @author Administrator
 *
 */
public class MyArrayList3 implements java.lang.Iterable<String> {
    private String[] elem ={"a","b","c","d","e","f","g"};
    private int size = elem.length;
    
    
    /**
     * 匿名内部类
     * @return
     */    
    public Iterator<String> iterator(){
        return new Iterator<String>(){
            private int cursor =-1;
            /**
             * 判断是否存在下一个元素
             * @return
             */
            public boolean hasNext(){        
                return cursor+1<size;
            }
            /**
             * 获取下一个元素
             */
            public String next(){
                cursor++; //移动一次
                return elem[cursor];
            }
            /**
             * 删除元素
             */
            public void remove(){
                //没有实现
            }
        
        };
    }
    
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        MyArrayList3 list = new MyArrayList3();
        Iterator<String> it =list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
            it.remove(); //删除元素
        }
        
        
        it =list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("增强for,必须实现java.lang.Iterable接口,重写iterator方法");
        for(String temp:list){
            System.out.println(temp);
            
        }
        
        
        
        
        
    }

}

 

 

 

 

package cn.bjsxt.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * this is a cat and that is a mice and where is the food?
 * 统计每个单词出现的次数
 * 
 * 存储到Map中
 * key :String 
 * value:自定义类型
 * 
 * "分拣" 思路
 * 1、为所有key创建容器
 *    之后容器中存放对应value
 * 2、第一次创建容器,并存放值value
 *    第二次之后,直接使用容器存放值
 * @author Administrator
 *
 */
public class Demo01 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String str ="this is a cat and that is a mice and where is the food";
        //分割字符串
        String[] strArray=str.split(" ");
        //存储到Map中
        Map<String,Letter>  letters = new HashMap<String,Letter>();
        for(String temp:strArray){            
            /*
             //1、为所有key创建容器         
            if(!letters.containsKey(temp)){
                Letter col = new Letter();
                col.setCount(1); //第一次值存放容器中
                letters.put(temp, col);
            }else{
                //2、     第二次之后,直接使用容器存放值
                Letter col =letters.get(temp); //直接使用容器
                col.setCount(col.getCount()+1);
            }*/
            Letter col = null;
            if(null==(col=letters.get(temp))){
                col = new Letter();
                col.setCount(1); //第一次值存放容器中
                letters.put(temp, col);
            }else{
                //2、     第二次之后,直接使用容器存放值                
                col.setCount(col.getCount()+1);
            }
        }
        //输出Map的值
        Set<String> keys = letters.keySet();
        for(String key:keys){
            Letter col =letters.get(key);
            System.out.println("字母:"+key+",次数"+col.getCount());
        }
        
        
    }
    public static void test1(){
        String str ="this is a cat and that is a mice and where is the food";
        //分割字符串
        String[] strArray=str.split(" ");
        //存储到Map中
        Map<String,Letter>  letters = new HashMap<String,Letter>();
        /*
        for(String temp:strArray){
            
             //1、为所有key创建容器
                  之后容器中存放对应value
             
            if(!letters.containsKey(temp)){
                letters.put(temp, new Letter());
            }
        }
        for(String temp:strArray){
            
            //  容器中存放对应value
            
            Letter col =letters.get(temp); //直接使用容器
            col.setCount(col.getCount()+1);
        }
        
        */
        for(String temp:strArray){
            
             //1、为所有key创建容器         
            if(!letters.containsKey(temp)){
                letters.put(temp, new Letter());
            }
            //2、     之后容器中存放对应value
            Letter col =letters.get(temp); //直接使用容器
            col.setCount(col.getCount()+1);
        }
        
        //输出Map的值
        Set<String> keys = letters.keySet();
        for(String key:keys){
            Letter col =letters.get(key);
            System.out.println("字母:"+key+",次数"+col.getCount());
        }
    }
}
package cn.bjsxt.map;

public class Letter {
    private String name;
    private int count;
    public Letter() {
        // TODO Auto-generated constructor stub
    }
    public Letter(String name, int count) {
        super();
        this.name = name;
        this.count = count;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    
    
    
}

 

package cn.bjsxt.map;

public class Student {
    private String name;
    private String no;
    private double score;
    public Student() {
    }
    public Student(String name, String no, double score) {
        super();
        this.name = name;
        this.no = no;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", no=" + no + ", score=" + score
                + "]";
    }

}
package cn.bjsxt.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 定义一个Student类,属性:name 姓名,classNumber 班号,score 成绩
   现在将若干Student对象放入List,请统计出每个班级的总分和平均分,分别打印出来
 以面向对象的思维解决
 * @author Administrator
 *
 */
public class MapDemo03 {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>();
        exam(list);        
        
        //统计        
        Map<String,ClassRoom> rooms = new HashMap<String,ClassRoom>();
        count(rooms,list);
        //打印
        printScore(rooms);
    }
    
    /**
     * 打印 总分与平均分
     */
    public static void printScore(Map<String,ClassRoom> rooms){
        Set<Map.Entry<String,ClassRoom>> entrySet =rooms.entrySet();
        Iterator<Map.Entry<String,ClassRoom>> it =entrySet.iterator();
        while(it.hasNext()){
            Map.Entry<String,ClassRoom> entry =it.next();
            ClassRoom room = entry.getValue();
            double avg = room.getTotal()/room.getStus().size();
            System.out.println("班号为:"+room.getNo()+",总分"+room.getTotal()+",平均分"+avg);
        }        
    }
    
    
    /**
     * 统计分数
     */
    public static void count(Map<String,ClassRoom> rooms,List<Student> list){
        for(Student stu:list){
            String no = stu.getNo();
            double score = stu.getScore();
            //根据班级编号 查看 Map是否存在该班级  分拣思路
            ClassRoom room = rooms.get(no);
            if(null==room){  //第一次
                room = new ClassRoom(no);
                rooms.put(no, room);
            }            
            //存储 总分
            room.setTotal(room.getTotal()+score);
            room.getStus().add(stu); //加入学生            
        }
    }
    
    
    /**
     * 现在将若干Student对象放入List
     * @param list
     */
    public static void exam(List<Student> list){
        list.add(new Student("a","001",80));
        list.add(new Student("b","001",80));
        list.add(new Student("a","002",80));
        list.add(new Student("c","003",80));
        list.add(new Student("d","003",80));
    }
}
package cn.bjsxt.map;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级
 * @author Administrator
 *
 */
public class ClassRoom {
    private String no;
    private List<Student> stus; //学生列表
    private double total; //总分
    public ClassRoom() {
        stus = new ArrayList<Student>();    
    }
    
    public ClassRoom(String no) {
        this();
        this.no = no;    
    }
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    public List<Student> getStus() {
        return stus;
    }
    public void setStus(List<Student> stus) {
        this.stus = stus;
    }
    public double getTotal() {
        return total;
    }
    public void setTotal(double total) {
        this.total = total;
    }
    
    
    
}
posted @ 2014-10-01 12:17  无名の辈  阅读(286)  评论(0编辑  收藏  举报