泛型,异常和日期

相互叠加泛型

List<Map<String,Object>>

  1. List<Map<String,Object>> list;适合前端页面遍历展示

  2. List list;需要创建一个News的新闻实体类对象,适合后端持久层

  3. 两个综合的集合对象特点是:

    (1)List集合的泛型不是独立的对象,而是news实体类对象,或者是Map

    (2)Map集合的key往往都是一样的

HashMap集合的深入

  1. 涉及到一个红黑树,来源是二叉树.用了红黑树算法完成了自动递增的效果,首先还是创建一个固定长度的[],这个”数组”集合的内存长度是可以动态赋值的

  2. List list=new ArrayList(3);

    数字的位置是给带参数构造函数赋值,初始化分配3个内存空间存储,当空间用完就自动再分配3个空间,实质是给内存的[]分配长度的

  3. 优势:自动递增长度,泛型灵活赋值也可以嵌套执行

  4. 缺点:集合应用在java中,不如[]的应用横向范围广

大案例

  1. 代码一:NewsModel

    package com.java.collections;
    /**
     * 新闻管理案例model层操作接口
     * @author Administrator
     *
     */
    import java.util.List;
    public interface NewsModel {
          
          
    	public List<News> SelectNews(News news);
    	//add new 新闻方法
    	//参数是一个新闻的记录
    	public boolean SaveNews(News news);
    	
    }
    
    
  2. 代码二:NewsModelImpl

    package com.java.collections;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class NewsModelImpl implements NewsModel {
          
          
    //全局变量,尽量别用new
    	List<News> list=null;
    	public NewsModelImpl() {
          
          
    		// TODO Auto-generated constructor stub
    }
    	@Override
    public List<News> SelectNews(News news) {
          
          
    	if(this.list==null || this.list.size()==0) {
          
          
    //null,或者是一个空new对象
    	this.GetAllNews();
    }
    List<News> lit=new ArrayList<News>();
    //判断查询条件:
    if(news!=null && !news.getTitle().equals(null)
    	&& !news.getBody().equals(null)) {
          
          
    //说明有查询条件
    for (News ne : this.list) {
          
          
    	if(ne.getTitle().equals(news.getTitle())
    		&& ne.getBody().equals(news.getBody())) {
          
          
    			lit.add(ne);
    	}
    }
    return lit;
    	}else //说明没有查询条件
    		return this.list;
    	}
    @Override
    public boolean SaveNews(News news) {
          
          
    if(this.list==null || this.list.size()==0) {
          
          
    //null,或者是一个空new对象
    	this.GetAllNews();
    }
    //比如判断主体主题是必须不能为null
    if(news.getTitle()==null || news.getBody()==null) {
          
          
    	System.out.println("主体主题是必须不能为null");
    	 return false;
    }else {
          
          
    this.list.add(news);
    System.out.println("add size:"+this.list.size());
    	return true;
    }		
    	}
         //独有的方法,获取全部新闻列表
    private void GetAllNews() {
          
          
    if(this.list==null) {
          
          
    	list=new ArrayList<News>();
    	}
    News news=new News("sina image", "sina title","sina video", "sina body");
    this.list.add(news);
    news=new News("sina1 image", "sina1 title","sina1 video", "sina1 body");
    this.list.add(news);
    news=new News("sina2 image", "sina2 title","sina2 video", "sina2 body");
    this.list.add(news);
    //        System.out.println(list.size());
    //        for (News news1 : list) {
          
          
    //			System.out.println(news1);
    //		}
    	}
    	public static void main(String[] args) {
          
          
    		// TODO Auto-generated method stub
    
    	}
    
    }
    
    
  3. 代码三:NewsController

    package com.java.collections;
    /**
     * 新闻管理案例controller层操作接口
     * @author Administrator
     *
     */
    import java.util.List;
    public interface NewsController {
          
          
       //show全部新闻方法,因为需要后期接受查询的参数,所以该方法也是有查询的参数的
    	//参数是News对象,也就是获取News对象的各个属性的值判断
    	public List<News> GetNews(String title,String body);
    	//add new 新闻方法
    	//参数是一个新闻的记录
    	public boolean AddNews(News news);
    	
    }
    
    
  4. 代码四:NewsControllerImpl

    package com.java.collections;
    import java.util.List;
    public class NewsControllerImpl implements NewsController {
          
          
     private NewsModel nm=null;
    public NewsControllerImpl() {
          
          
    	if(nm==null) {
          
          
    //NewsModel nm=new NewsModelImpl(); 抛空指针异常
    	nm=new NewsModelImpl();
    	}
    }
        /**
         * 参数是查询条件,news对象,所以默认是查询title和
         * body
         */
    @Override
    public List<News> GetNews(String title,String body) {
          
          
    if(title!=null && body!=null) {
          
          
    	News news=new News(null, title, null, body);
    		return this.nm.SelectNews(news);	
    }else {
          
          
    	return this.nm.SelectNews(null);
    		}		
    	}
    @Override
    public boolean AddNews(News news) {
          
          
    return this.nm.SaveNews(news);
    }
    public static void main(String[] args) {
          
          
    // 控制台接收的方法。
    NewsController nc=new NewsControllerImpl();
    //搜索条件       
    //List<News> ls=nc.GetNews("sina1 title","sina1 body");
    //add
    News ne=new News("sina9 image", "sina9 title","sina9 video", "sina9 body");
    System.out.println(nc.AddNews(ne)?"添加ok":"添加error");
    //查询结果。
    List<News> ls=nc.GetNews(null,null);
      for (News news : ls) {
          
          
    	System.out.println(news);
      }
    }
    }
    
    
  5. 代码五:News

    package com.java.collections;
    import java.io.Serializable;
    public class News implements Serializable{
          
          
    private static final long serialVersionUID = 1L;
    public News() {
          
          
    		
    }
    private String image,title,video,body;
     public News(String image,String title,String video,String body) {
          
          
        	this.body=body;
        	this.image=image;
        	this.title=title;
            this.video=video;	
        }
    	public String getImage() {
          
          
    		return image;
    	}
    	public void setImage(String image) {
          
          
    		this.image = image;
    	}
    	public String getTitle() {
          
          
    		return title;
    	}
    	public void setTitle(String title) {
          
          
    		this.title = title;
    	}
    	public String getVideo() {
          
          
    		return video;
    	}
    	public void setVideo(String video) {
          
          
    		this.video = video;
    	}
    	public String getBody() {
          
          
    		return body;
    	}
    	public void setBody(String body) {
          
          
    		this.body = body;
    	}
        @Override
        public String toString() {
          
          
    return "Image:"+getImage()+",Title:"+getTitle()+",body:"+getBody()+",视频:"+getVideo();
        }
    }
    
    
  6. 代码六:NewsDemo

    package com.java.collections;
    import java.util.*;
    /**
     * 嵌套集合综合案例,
     * 新闻列表
     * @author Administrator
     *
     */
    public class NewsDemo {
          
          
    	public NewsDemo() {
          
          
    		
    	}
    	/**
    	 * 综合案例:
    	 * 1.List集合的泛型往往不是一个独立的对象,而是一个news实体类对象,或者是Map集合
    	 * 2.Map集合的key往往都是一样的,原因是真正的遍历目的地是前端的页面所以页面中需要匹配key
    	 * 去对号入座的获取对应的value
    	 */
        public static void number1(){
          
          
        	//新闻列表综合案例前期,集合对象一,适合前端展示。
            List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
            //创建多个Map集合new对象
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("image", "sina news images");
            map.put("title", "sina news titles");
            map.put("video", "sina news videos");
            map.put("body", "sina news 主题文字");
            //封装给list,完成了一个map集合的封装
            list.add(map);
            
            map=new HashMap<String, Object>();
            map.put("image", "sohu news images");
            map.put("title", "sohu news titles");
            map.put("video", "sohu news videos");
            map.put("body", "sohu news 主题文字");
            //封装给list,完成了一个map集合的封装
            list.add(map);
            
            map=new HashMap<String, Object>();
            map.put("image", "凤凰 news images");
            map.put("title", "凤凰 news titles");
            map.put("video", "凤凰 news videos");
            map.put("body", "凤凰 news 主题文字");
            //封装给list,完成了一个map集合的封装
            list.add(map);
            
            for (Map<String, Object> map1 : list) {
          
          
    			Iterator<String> itkey=map1.keySet().iterator();
    			while (itkey.hasNext()) {
          
          
    				String key = itkey.next();
    				System.out.println(key+"  /  "+map1.get(key));
    			}
    		}
        }
        public static void number2(){
          
          
        	//新闻列表综合案例前期,集合对象2,适合DB持久层框架对接。
    List<News> list=new ArrayList<News>();
    News news=new News("sina image", "sina title","sina video", "sina body");
    list.add(news);
     news=new News("sina1 image", "sina1 title","sina1 video", "sina1 body");
    list.add(news);
     news=new News("sina2 image", "sina2 title","sina2 video", "sina2 body");
    list.add(news);
            for (News news1 : list) {
          
          
    			System.out.println(news1);
    		}
        }
    	public static void main(String[] args) {
          
          
    //		number2();
    		/*
    		 * 数字的位置是给带参数构造函数赋值:
    		 * 初始化分配3个内存空间存储,list map,每当空间用完,就自动再分配3个空间。
    		 * 实质是给内存的[]分配长度的。
    		 */
    //		List<String> list=new ArrayList<String>(3);
    //		Map<String, Object> map=new HashMap<String, Object>(10);
    		
    	}
    
    }
    
    

异常管理

  1. Try catch finally

  2. 所有异常错误,都是源于所有异常种类的父类对象Exception

  3. Try包裹程序执行,抛异常执行catch,无论是否抛异常都会执行finally

  4. 方法抛异常,省略try catch编写

  5. 异常代码:

    package com.java.collections;
    /**
     * 异常管理
     * 属于错误的类型之一:运行时异常错误提示的操作对象
     * @author Administrator
     *
     */
    public class Excepts {
          
          
    	public Excepts() {
          
          
    //		try {
          
          
    //			int[] a=new int[5];
    //			a[5]=23; //如果抛异常,如同执行了return
    //			System.out.println(123);
    //		} catch (ArrayIndexOutOfBoundsException e) {
          
          
    //			System.out.println("except----");
    //			System.out.println(e);
    //		}
    	}
    	//如果方法抛异常,那么需要调用该方法需要try catch包裹,但是不是所有的Exception都需要try catch
    	public void exce() throws NoSuchMethodException{
          
          
    		int[] a=new int[5];
    		a[5]=23; //如果抛异常,如同执行了return
    		System.out.println(123);
    	}
    	public static void main(String[] args) {
          
          
    		try {
          
          
    			new Excepts().exce();
    		} catch (NoSuchMethodException e) {
          
          
    			e.printStackTrace();
    		}finally {
          
          
    			System.out.println("final");
    		}
    //		exce();
    	}
    
    }
    
    
  6. 获取当前日期:

    package com.java.collections;
    
    import java.text.*;
    import java.util.*;
    
    
    public class Dates {
          
          
    
    	public Dates() {
          
          
    		//Date获取当前日期
    		date=new Date();
    //		System.out.println(date);  Wed Mar 10 17:04:41 CST 2021
    		//日期格式化显示为需要的格式效果
    		
    	}
    	private Date date=null;
        private SimpleDateFormat dateFor=null;
        
    public void GetDate() {
          
          
    //如果按照需要的格式输出,那么Simpl..对象赋值对应的格式
    this.dateFor=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    System.out.println(this.dateFor.format(new Date()));
     }
      
    //中文格式
    public void GetDate1() {
          
          
    //如果按照需要的格式输出,那么Simpl..对象赋值对应的格式
    this.dateFor=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒");    	System.out.println(this.dateFor.format(date));
    }
       
    //数字时间
    public void GetDate2() {
          
          
    //如果按照需要的格式输出,那么Simpl..对象赋值对应的格式
    this.dateFor=new SimpleDateFormat("yyyyMMddHHmmssSSS");    	System.out.println(this.dateFor.format(date));
    }
    
    //日历
    public void CalendarDemo() {
          
          
    Calendar calendar = new GregorianCalendar();
    // 实例化Calendar类对象
    System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
        
    System.out.println("MONTH: " + (calendar.get(Calendar.MONTH) + 1));
        
    System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
        
    System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
        
    System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
        
    System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
        
    System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
        }
    public void DateFormatDemo() {
          
          
    //都属于java操作日期 日历格式化的一个工具类
    DateFormat df1 = null ;
    // 声明一个DateFormat
    DateFormat df2 = null ;		
    // 声明一个DateFormat
    df1 = DateFormat.getDateInstance() ;
    // 得到日期的DateFormat对象
    df2 = DateFormat.getDateTimeInstance() ;
    // 得到日期时间的DateFormat对象
    System.out.println("DATE:"+df1.format(new Date())) ; // 按照日期格式化
    System.out.println("DATETIME"+df2.format(new.Date());
    // 按照日期时间格式化
    }
    public void DateFormatDemo1() {
          
          
    //通过此类可以将Date类进行合理的格式化操作,采用默认的格式化
    //也可以通过Locale对象指定要显示的区域,指定的区域是中国
    DateFormat df1 = null ;
    // 声明一个DateFormat
    DateFormat df2 = null ;
    // 声明一个DateFormat
    df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,
    new Locale("zh","CN")) ;
    // 得到日期的DateFormat对象
    df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,
    new Locale("zh","CN")) ;	
    // 得到日期时间的DateFormat对象
    System.out.println("DATE:"+df1.format(new Date())) ; // 按照日期格式化
    System.out.println("DATETIME:" + df2.format(new Date())) ;
    //按照日期时间格式化
    }
    	public static void main(String[] args) {
          
          
    		// TODO Auto-generated method stub
            Dates d=new Dates();
            d.DateFormatDemo1();
    //        for (int i = 0; i < 100; i++) {       	 
    //        	try {
          
          
    //        		d.GetDate();
    //				Thread.sleep(1000);
    //			} catch (InterruptedException e) {
          
          
    //				// TODO Auto-generated catch block
    //				e.printStackTrace();
    //			}
    //		}
           /**
            * 日期格式,脚本实际开发更加实用。
            */
        }
    }
    
    

猜你喜欢

转载自blog.csdn.net/qq_52332852/article/details/114644314
今日推荐