zoukankan      html  css  js  c++  java
  • 删除集合元素Collection ,remove()

    package seday11;
    /**
    * @author xingsir
    */
    public class coordinate {

    private int x;
    private int y;
    /*
    * 右键点-Source-点 -generate constructor using fields,选择要生成的属性
    * 这个选项自动生成带参数的 构造函数
    */
    public coordinate(int x, int y) {
    super();
    this.x = x;
    this.y = y;
    }
    /*
    * 右键点-Source-点 -generate getters and setters,选择要生成的属性
    */
    public int getX() {
    return x;
    }
    public void setX(int x) {
    this.x = x;
    }
    public int getY() {
    return y;
    }
    public void setY(int y) {
    this.y = y;
    }
    public String toString() {
    return"("+x+","+y+")";
    }

    /*
    * 右键点-Source-点 -generate hashCode() and equals(Object obj),选择要生成的属性
    * 这个选项自动生成
    */
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + x;
    result = prime * result + y;
    return result;
    }
    // 右键点-Source-点 -
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (getClass() != obj.getClass())
    return false;
    coordinate other = (coordinate) obj;
    if (x != other.x)
    return false;
    if (y != other.y)
    return false;
    return true;
    }

    }

    //======================================================================

    package seday11;

    import java.util.ArrayList;
    import java.util.Collection;

    /**
    * @author xingsir
    * 删除集合元素
    * boolean remove()从集合中删除给定元素,删除的是集合中与给定元素equals比较为true的元素。
    */
    public class CollectionDemo2 {

    public static void main(String[] args) {
    Collection c= new ArrayList();
    c.add(new coordinate(1, 1));
    c.add(new coordinate(2, 2));
    c.add(new coordinate(3, 3));
    c.add(new coordinate(4, 4));
    c.add(new coordinate(5, 5));
    System.out.println(c);

    coordinate p =new coordinate(5,5);
    c.remove(p);
    System.out.println(c);


    }

    }

    //=========================================================================================

    package seday11;

    import java.util.ArrayList;
    import java.util.Collection;

    /**
    * @author xingsir
    * 集合只能存放引用类型元素,而且保存的也是元素的引用(地址)
    */
    public class CollectionDemo4 {

    public static void main(String[] args) {
    Collection c=new ArrayList();
    coordinate p=new coordinate(3, 3);
    c.add(p);//添加

    /*执行结果:
    p:(3,3)
    c:[(3,3)]
    p:(4,3)
    c:[(4,3)]
    */
    System.out.println("p:"+p);
    System.out.println("c:"+c);

    p.setX(4);//设置x为4
    System.out.println("p:"+p);
    System.out.println("c:"+c);

    }

    }

  • 相关阅读:
    硬币游戏 Project Euler 232
    屏幕空间的近似全局光照明(Approximative Global Illumination in Screen Space)
    四维之美
    vertex texture fetching in HLSL, and heightfield normal calculation
    一个VS小插件(跳出括号)
    我的算法书籍收藏
    Algorithms.算法概论.习题答案
    UML用例图教程详解
    大连理工大学软件学院博客地址
    快递查询API,我推荐“爱快递”
  • 原文地址:https://www.cnblogs.com/xingsir/p/12084954.html
Copyright © 2011-2022 走看看