List接口的使用(对集合元素进行增、删、改、查、排序)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_44213634/article/details/99286493
package ListInterfaceImplementation;
/**
 * 角色的自定义类
 * @author 李*文
 * @version 1.8
 * @date 2019年8月11日 上午10:11:45
 * @content JAVA代码
 * @motto 代码千万条,可读第一条。代码不规范,error多两行。
 */
public class Role implements Comparable<Role>{
    private int hp;//经验值
    private int health;//健康值
    private String name;//名称
    private String title;//称号
	public Role(int hp, int health, String name, String title) {
		this.hp = hp;
		this.health = health;
		this.name = name;
		this.title = title;
	}
	/**
	 * 重写equals方法  进行元素属性值的比较
	 * 来避免由于不同的地址 无法进行两个对象相等之间的判断
	 * 如果传入的对象是null或者传入的对象不是同一个对象
	 */
     @Override
    public boolean equals(Object obj) {
    	 
    	if(obj==null||!(obj instanceof Role))
    	{
    		return false;
    	}
   
      Role role=(Role)obj;
    		
    	return this.getName().equals(role.getName());
    	
    }
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	@Override
	public int compareTo(Role role) {
		 if(this.hp>role.getHp())
		 {
			 return 1;
		 }
		 else if(this.hp<role.getHp())
		 {
			 return -1;
		 }
		 else
		 {
			 return 0;
		 }
		
	
	}
    
	}
 
package ListInterfaceImplementation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
/**
 * 角色的相关操作的实现-增删改查
 * @author 李*文
 * @version 1.8
 * @date 2019年8月11日 上午10:14:56
 * @content JAVA代码
 * @motto 代码千万条,可读第一条。代码不规范,error多两行。
 */
public class RoleBiz {
    List<Role> roleList=null;
    //创建构造方法
    public RoleBiz()
    {
    	roleList=new ArrayList<Role>();
    }
    
    //创建相关的功能方法
    /**
     * 向集合中添加元素
     * @param role 角色的对象
     */
     public void add(Role role)
     {
    	 roleList.add(role);
    	 
     }
       /**
        * 对传入的对象进行删除
        * @param role 传入的对象
        */
       public void delete(Role role)
       {
    	   // 寻找role对象的下标
    	   int index=-1;
   	        //由于比较的是两个
    	   if((index=roleList.indexOf(role))==-1)
    	   {
    		   System.out.println("删除失败!");
    		        return;
            }
               //进行删除
    		   roleList.remove(role);
    	 }
       /**
        * 对传入的对象 用重写的equals的方法进行判断对象的名称是否相等 
        *再修改对象的属性
        * @param role 传入的对象
        */
        public void update(Role role)
        {
        	int index=-1;
        	if((index=roleList.indexOf(role))==-1)
        	{
        		System.out.println("修改失败");
        		 return ;
        	}
        	//在集合中重新对对象进行设置
        	roleList.set(index,role);
                   
        }
        /**
         * 查询
         * @param role 传入的对象
         * @return 对象在集合中的下标
         */
        public int  search(Role role)
        {
        	int index=-1;
        	int i=0;
        	for (;i<roleList.size();i++) {
			     if(roleList.get(i)==role)
			     {
			    	 index=i;
			    	 break;
			     }
			}
        	if(i==roleList.size())
        	{
        		System.out.println("查询失败!");
        	       System.exit(0);
        	}
        	return  index;
        	
        }
        
       public void sort()
       {
    	    //使用实现compareable接口对其进排序
    	    //Collections.sort(roleList);
    	    //对集合元素进行反转
    	    //Collections.reverse(roleList);
    	    //使用自定义方法进行比较
    	   Collections.sort(roleList, new compareto_helth());
    	   //打乱排序
    	   Collections.shuffle(roleList);
    	   
       }
        /**
          * 进行集合的遍历
          */
       public void show()
       {
    	   Iterator<Role> list=roleList.iterator();
    	   System.out.println("名称"+"\t"+"  "+"称号"+"\t"+"\t"+"健康值"+"\t"+"\t"+"经验值");
    	   while(list.hasNext())
    	   {    Role role=list.next();
    		   System.out.println(role.getName()+"\t"+
    				   role.getTitle()+"\t"+"\t"+role.getHealth()+"\t"+"\t"+role.getHp());
    	   }
    	   System.out.println();
       }
       
}



/*class compareto_helth implements Comparator<Role>
{

	@Override
	public int compare(Role role1, Role role2) {
	  if(role1.getHealth()>role2.getHealth())  
	  {
		  return 1;
	  }
	  else if(role1.getHealth()<role2.getHealth())  
	  {
		  return -1;
	  }
	  else
		return 0;
	}
}*/
package ListInterfaceImplementation;

public class RoleTest {

	public static void main(String[] args) {
		 RoleBiz roleBiz=new RoleBiz();
		    //对其进行添加元素
		   Role role1=new Role(1006, 504, "张三", "小李飞刀");
		   Role role2=new Role(1001, 509, "张二", "小李飞锅");
		   Role role3=new Role(1009, 505, "张一", "小李飞碟");
		   Role role4=new Role(1005, 506, "张四", "小李飞盘");
		   Role role5=new Role(1004, 501, "张五", "小李飞碗");
		   roleBiz.add(role1);
		   roleBiz.add(role2);
		   roleBiz.add(role3);
		   roleBiz.add(role4);
		   roleBiz.add(role5);
		   roleBiz.show();
		   //进行查询操作
		     System.out.println("查询的结果为:"+roleBiz.search(role3));
		     role5=new Role(1010, 999, "张二", "小李飞锅");
		   //进行修改操作
		     roleBiz.update(role5);
	       //进行删除操作
		     roleBiz.delete(role2);
		     System.out.println("排序后的结果为:");
		     roleBiz.sort();
		     roleBiz.show();
	}

}

猜你喜欢

转载自blog.csdn.net/qq_44213634/article/details/99286493