zoukankan      html  css  js  c++  java
  • 从入门到放弃的第三周(a'pi)......day.12。。。。。Iterable,Comparable,Clonable;

    Iterable,Comparable,Comparator,Clonable接口,集合框架: Collection接口,List接口,ArrayList,LinkedList


    1,Iterable:提供的可迭代的能力

     


    实现其唯一的抽象方法:
    public Iterator iterator()

    Iterator接口表示迭代器。其中有两个抽象方法:
    public boolean hasNext():判断有没有下一个
    public T next():获取到下一个

    设计模式中的迭代器模式
    Collection的遍历

     

     

    2,Comparable,一般是某个具有比较能力的实体类实现该接口
    使用int compareTo(T t):
    大于t,返回正数,等于t,返回0 ,否则,返回负数

     

     


    3,Comparator,一般单独实现该接口,提供比较的策略,体现是策略模式

     


    使用int compareTo(T t1,T t2)
    t1>t2,返回正数,t1等于t2,返回0 ,否则,返回负数

    策略模式
    在需要保证顺序的集合中存储的元素是具有比较能力

     

     


    4,Clonable接口,克隆

     


    实体类除了继承Clonable接口,表示其具有克隆的能力,同时重写Object的clone方法,一般使用public修饰

    浅克隆与深克隆
    在克隆对象中有引用类型的属性,浅克隆只会简单的克隆其地址,深克隆会将引用类型的属性克隆出一份独立的空间

    设计模式之原型模式

     

     


    5,集合框架

     


    处理若干元素(单个,键值对),也称为容器

     

     

    课堂任务:
    1,使用ArrayList存储若干学生的姓名,编写一个方法,找出集合中姓名长度大于5的,将其放在集合中返回

    public class Job1 {
    public List<Student> max(List<Student> s){
    	  List<Student> name=new ArrayList<>();
                  for(int i=0;i<s.size();i++){
                	 if(s.get(i).getName().length()>=5){
                		 name.add(s.get(i));  		 
                	 }      		
                  }
    		return name		
    	}
    }
    

      2,编写方法,找出存储整数的List中的最大值

           

    public class Job2 {
    	public int max(List<Student> s){
    int max=s.get(0).getAge(); for(int i=0;i<s.size();i++){ if(max<s.get(i).getAge()){ max=s.get(i).getAge(); } }
    return max; } }

      


    3,编写方法,计算两个List的交集

    public class Job3 {
           public static List<Integer> jiaoji(List<Integer> a,List< Integer> b){
        	   List<Integer> x=new ArrayList<>();
        	   List<Integer> y1=new ArrayList<>();
        	   List<Integer> z1=new ArrayList<>();
        	   
        	   int y=(a.size()<b.size()?a.size():b.size());
        	   int z=(a.size()>=b.size()?a.size():b.size());
        	   y1=(a.size()>=b.size()?b:a);
        	   z1=(a.size()>=b.size()?a:b);
        	   
        	   for(int i=0;i<y;i++){
        		   for(int j=0;j<z;j++){
        			   if(y1.get(i)==z1.get(j)){
        				   x.add(y1.get(i));
        				   }   			   
        		   }
        	 }  	   
    		return x;  
           }             
    	public static void main(String[] args) {
    		List<Integer> x=new ArrayList<>();
    		List<Integer> y=new ArrayList<>();		
    		x.add(1);
    		x.add(4);x.add(3);x.add(7);x.add(5);x.add(12);		
    		y.add(1);
    		y.add(12);
    		y.add(21);
    		y.add(231);
    		y.add(41);
    		y.add(121);
    		y.add(21);		
                 x=jiaoji(x, y);           
    			for (int i = 0; i < x.size(); i++) {
    				System.out.println(x.get(i));
    			}		
    	}	
    }
    

      


    4,产生10个1-100的随机数,并放到一个数组中,把数组中大于等于10的数字放到一个list集合中,并打印到控制台

    public class Job4 {
    	
    	public static List<Integer> panduan(List<Integer> s){
    		List<Integer> x=new ArrayList<Integer>();
    		for(int i=0;i<s.size();i++){
    			if(s.get(i)<10){
    				x.add(s.get(i));
    			}
    		}
    		return x;
    	}		
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
                 List<Integer> x=new ArrayList<Integer>();
                 for(int i=0;i<10;i++){
                	int  y=(int)(Math.random()*100+1);
                	 x.add(y);           	 
                 }
                 for (Integer integer : x) {
    				System.out.println(integer);
    			}        
                x=panduan(x);           
                for (Integer integer : x) {
    				System.out.println(integer);
    			}          
    	}
    }
    

      


    5,将User集合ArrayList按照name的长度进行升序排序

    多做题就对了。。。。

  • 相关阅读:
    tomcat调优
    使用Docker部署Spring Boot项目
    java配置ueditor中解决“未找到上传文件”错误提示
    java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
    spring01
    android中的wrap_content,match_parent, fill_parent
    RPC和http
    Failed to read artifact descriptor for xxx
    Error processing condition on org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafWebMvcConfiguration.resourceUrlEncodingFilter
    springboot的自动配置
  • 原文地址:https://www.cnblogs.com/suxiao666/p/11372705.html
Copyright © 2011-2022 走看看