关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值

对于基本数据类型和常见的抽象数据类型不做考虑
针对的是由程序编写者创建的抽象数据类型

通过查看API文档,提供了一个remove(Object O)方法
关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值_第1张图片
**

一. 首先想到的是逐个比较不同节点里的数据,list.remove(index i);

**

import java.util.*;

/**
 *
 */

public class Main {
    public static class Node{
        int num;
        String val;
        private Node(int num, String val){
            this.num = num;
            this.val = val;
        }
    }



    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        //LinkedList list = new LinkedList<>();
        list.add(new Node(5,"aa"));
        list.add(new Node(6,"aa"));
        list.add(new Node(7,"aa"));
        list.add(new Node(8,"aa"));
        list.add(new Node(9,"aa"));
        list.add(new Node(10,"aa"));
        list.add(new Node(11,"aa"));
        list.add(new Node(5,"aa"));

        for(Node c:list){
            System.out.print(c.num + c.val +" ");
        }
        System.out.println();
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).num == 5 && list.get(i).val.equals("aa")){
                list.remove(i);
            }
        }
        for(Node c: list){
            System.out.print(c.num + c.val + " ");

        }
    }
}

结果:删除了num== 5,val == “aa”的所有节点
关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值_第2张图片

其实这个是不对的,有个细节没有考虑到,测试一下下面的数据就知道了

        list.add(new Node(5,"aa"));
        list.add(new Node(5,"aa"));
        list.add(new Node(6,"aa"));
        list.add(new Node(7,"aa"));
        list.add(new Node(8,"aa"));
        list.add(new Node(9,"aa"));
        list.add(new Node(10,"aa"));
        list.add(new Node(10,"aa"));
        list.add(new Node(11,"aa"));

结果:
关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值_第3张图片

for(int i = 0; i < list.size(); i++){
            if(list.get(i).num == 5 && list.get(i).val.equals("aa")){
                list.remove(i);
                /**修改的部分*/
                i--;
            }
        }

结果:
在这里插入图片描述

增加更该节点数据的功能,需要在节点类里加入get(),set()方法;

import java.util.*;

/**
 *
 */

public class Main {
    public static class Node{
        int num;
        String val;
        private Node(int num, String val){
            this.num = num;
            this.val = val;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public String getVal() {
            return val;
        }

        public void setVal(String val) {
            this.val = val;
        }
    }



    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        list.add(new Node(5,"aa"));
        list.add(new Node(6,"aa"));
        list.add(new Node(7,"aa"));
        list.add(new Node(8,"aa"));
        list.add(new Node(9,"aa"));
        list.add(new Node(10,"aa"));
        list.add(new Node(11,"aa"));
        list.add(new Node(5,"aa"));

        for(Node c:list){
            System.out.print(c.num + c.val +" ");
        }
        System.out.println();
        /**
     * 更改地方
     */
    for(int i = 0; i < list.size(); i++){
        if(list.get(i).num == 5 && list.get(i).val.equals("aa")){
            list.remove(i);
            /**更改的地方**/
            i--;
            /**更改地方**/
            continue;
        }
        if(list.get(i).num == 10 && list.get(i).val.equals("aa")){
            list.get(i).setVal("uu");
        }
    }
        for(Node c: list){
            System.out.print(c.num + c.val + " ");

        }
    }
}

关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值_第4张图片
**

二.删除第一个搜索到的节点

list.remove(Object o);

 import java.util.*;

/**
 *
 */

public class Main {
    public static class Node{
        int num;
        String val;
        private Node(int num, String val){
            this.num = num;
            this.val = val;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public String getVal() {
            return val;
        }

        public void setVal(String val) {
            this.val = val;
        }
    }



    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        Node node1 = new Node(5,"aa");
        Node node2 = new Node(6,"bb");
        Node node3 = new Node(7,"cc");
        Node node4 = new Node(8,"cc");
        Node node5 = new Node(9,"aa1");
        Node node6 = new Node(10,"cc");

        list.add(node1);
        list.add(node2);
        list.add(node3);
        list.add(node4);
        list.add(node5);
        list.add(node6);
        list.add(node1);


        for(Node c:list){
            System.out.print(c.num + c.val +" ");
        }
        System.out.println();
        /**更改地方*/
        list.remove(node1);
        for (Node node0 : list){
            System.out.print(node0.num +  node0.val + " ");
        }

    }
}

结果:只删除了搜索到的第一个Node1节点
关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值_第5张图片

**

三,重写equel();删除所有节点Node

**

import java.util.ArrayList;
/**
 *
 */

public class Main {
    public static class Node{
        private int num;
        private String val;
        public Node(int num, String val){
            this.num = num;
            this.val = val;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public String getVal() {
            return val;
        }

        public void setVal(String val) {
            this.val = val;
        }

        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            if(this == obj) return true;
            if(obj == null){
                return false;
            }
            if(obj instanceof Node){
                Node node = (Node) obj;
                if(node.num == this.num  && node.val.equals(this.val)){
                    return true;
                }
                return false;
            }
            return false;
        }

    }



    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        list.add(new Node(5,"aa"));
        list.add(new Node(6,"aa"));
        list.add(new Node(7,"aa"));
        list.add(new Node(8,"aa"));
        list.add(new Node(9,"aa"));
        list.add(new Node(10,"aa"));
        list.add(new Node(5,"aa"));

        for(Node c:list){
            System.out.print(c.num + c.val +" ");
        }
        System.out.println();


        Node delete = new Node(5,"aa");
        for (int i = 0; i < list.size(); i++){
            if(delete.equals(list.get(i))) {
                list.remove(i);
                i--;
            }
        }


        for (Node node0 : list){
            System.out.print(node0.num +  node0.val + " ");
        }
        System.out.println();

    }
}

结果:删除所有符合删除条件的节点
关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值_第6张图片
**

四.不重写equal();remove()方法失效

**

import java.util.ArrayList;
/**
 *
 */

public class Main {
    public static class Node{
        private int num;
        private String val;
        public Node(int num, String val){
            this.num = num;
            this.val = val;
        }

//        public int getNum() {
//            return num;
//        }
//
//        public void setNum(int num) {
//            this.num = num;
//        }
//
//        public String getVal() {
//            return val;
//        }
//
//        public void setVal(String val) {
//            this.val = val;
//        }
//
//        @Override
//        public int hashCode() {
//            return super.hashCode();
//        }
//
//        @Override
//        public boolean equals(Object obj) {
//            if(this == obj) return true;
//            if(obj == null){
//                return false;
//            }
//            if(obj instanceof Node){
//                Node node = (Node) obj;
//                if(node.num == this.num  && node.val.equals(this.val)){
//                    return true;
//                }
//                return false;
//            }
//            return false;
//        }

    }



    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        list.add(new Node(5,"aa"));
        list.add(new Node(6,"aa"));
        list.add(new Node(7,"aa"));
        list.add(new Node(8,"aa"));
        list.add(new Node(9,"aa"));
        list.add(new Node(10,"aa"));
        list.add(new Node(5,"aa"));

        for(Node c:list){
            System.out.print(c.num + c.val +" ");
        }
        System.out.println();


        Node delete = new Node(5,"aa");
        list.remove(delete);
//        for (int i = 0; i < list.size(); i++){
//            if(delete.equals(list.get(i))) {
//                list.remove(i);
//                i--;
//            }
//        }


        for (Node node0 : list){
            System.out.print(node0.num +  node0.val + " ");
        }
        System.out.println();

    }
}

结果:
关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值_第7张图片

重写equal();

import java.util.ArrayList;
/**
 *
 */

public class Main {
    public static class Node{
        private int num;
        private String val;
        public Node(int num, String val){
            this.num = num;
            this.val = val;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public String getVal() {
            return val;
        }

        public void setVal(String val) {
            this.val = val;
        }

        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            if(this == obj) return true;
            if(obj == null){
                return false;
            }
            if(obj instanceof Node){
                Node node = (Node) obj;
                if(node.num == this.num  && node.val.equals(this.val)){
                    return true;
                }
                return false;
            }
            return false;
        }

    }



    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        list.add(new Node(5,"aa"));
        list.add(new Node(6,"aa"));
        list.add(new Node(7,"aa"));
        list.add(new Node(8,"aa"));
        list.add(new Node(9,"aa"));
        list.add(new Node(10,"aa"));
        list.add(new Node(5,"aa"));

        for(Node c:list){
            System.out.print(c.num + c.val +" ");
        }
        System.out.println();


        Node delete = new Node(5,"aa");
        list.remove(delete);
//        for (int i = 0; i < list.size(); i++){
//            if(delete.equals(list.get(i))) {
//                list.remove(i);
//                i--;
//            }
//        }


        for (Node node0 : list){
            System.out.print(node0.num +  node0.val + " ");
        }
        System.out.println();

    }
}

结果:
关于ArrayList LinkedList remove()方法的一点思考,更改或删除抽象数据类型的值_第8张图片

你可能感兴趣的:(学习总结,remove(),arraylist,linkedlist,构造节点)