如何在Java中使用ArrayList移除不等于对象 [英] How to remove not equals Objects using ArrayList in Java

查看:84
本文介绍了如何在Java中使用ArrayList移除不等于对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个ArrayList书籍,并且仅当它们不同时才想删除一定数量的书籍(使用等号).我该如何解决这个问题?

例如,如果我有3本不同的书,而我的 quantitytoremove 是3.则必须删除这3本书.假设 quantitytoremove 不能高于数组中不同书的数量.

我尝试了类似的方法,但是我知道我写的东西有问题.流中是否有可用的功能,否则可以为我提供帮助?

 <代码>公共购物篮removeDifferent(int Quantitytoremove){删除的int = 0;for(int i = 0; i< this.numberBooks&< lt; quantitytoremove; i ++){for(int j = i + 1; j< this.numberBooks& Removed< quantitytoremove; j ++){如果(!(this.BooksArray.get(i).equals(this.BooksArray.get(j)))){图书Book_i = this.BooksArray.get(i);图书Book_j = this.BooksArray.get(j);this.BooksArray.remove(Book_i);this.BooksArray.remove(Book_j);已移除=已移除+2;this.numberBooks = numberBooks-2;i = 0; j = 1;}}}} 

解决方案

首先,类Foo中的@Override equals()决定使两个对象相等的属性是什么,并使用 recursion

删除.>

说明递归的工作原理

qn 表示已完成删除的次数

起点 i = 0 j = 1 并递归直到达到基本条件

步骤1,基本条件:如果 i j 超出了数组的大小,或者超出了 qn quantityToRemove 返回 qn

步骤2,检查相等性,如果 i j 的数组元素不相等,则删除并递增 qn 并递减 j 以避免丢失元素

对所有 i 进行

第3步,并递增 j 重复第3步,直到达到基本条件

第4步递增 ++ i

步骤5 以增量 i 开始新的递归,并从 i + 1

开始 j

在撤消操作完成后,检查 qn 是否未超过 quantityToRemove ,如果包含1本书,则从数组中删除.这是当您有不同的4本书并希望将其全部删除的情况下

...

跟踪

i = 0,j = 1,2,....,N

i = 1,j = 2,3,....,N

i = 2,j = 3,4,....,N

i = 3,j = 4,5,....,N

...

 静态类图书{int id;字符串标题;公共图书(int id,字符串标题){this.id = id;this.title =标题;}@Overridepublic boolean equals(Object obj){其他其他=(Book)obj;返回this.title.equals(other.title);}@Override公共字符串toString(){返回id +:" +标题;}}公共静态void main(String [] a){图书b1 =新图书(1,"b1");书籍b2 =新书籍(2,"b2");图书b3 =新图书(3,"b3");书籍b4 =新书籍(4,"b4");书籍b5 =新书籍(1,"b1");列表<书>list = new ArrayList<>();list.add(b1);list.add(b2);list.add(b3);list.add(b4);list.add(b5);list.add(new Book(1,"b1"));list.add(new Book(1,"b1"));list.add(new Book(1,"b1"));System.out.println(list);removeDifferent(list,2);System.out.println(list);}静态无效的removeDifferent(List< Book> booksArray,int QuantityToRemove){int qn = removeDifferent(booksArray,QuantityToRemove,0,0,1);if(booksArray.size()== 1&& qn< quantityToRemove)booksArray.remove(0);}静态int removeDifferent(List< Book> booksArray,int QuantityToRemove,int qn,int i,int j){如果(i> = booksArray.size()|| j> = booksArray.size()|| qn> = QuantityToRemove)返回qn;如果(!booksArray.get(i).equals(booksArray.get(j))){booksArray.remove(j);j--;qn ++;}qn = removeDifferent(booksArray,QuantityToRemove,qn,i,1 + j);++ i;qn = removeDifferent(booksArray,QuantityToRemove,qn,i,i + 1);返回qn;} 

,输出

  [1:b1,2:b2,3:b3,4:b4,1:b1,1:b1,1:b1,1:b1][1:b1,4:b4,1:b1,1:b1,1:b1,1:b1] 

,为此输入

 书籍b1 =新书籍(1,"b1");书籍b2 =新书籍(2,"b2");图书b3 =新图书(3,"b3");书籍b4 =新书籍(4,"b4");列表<书>list = new ArrayList<>();list.add(b1);list.add(b2);list.add(b3);list.add(b4);System.out.println(list);removeDifferent(list,4);System.out.println(list); 

,输出

  [] 

If I have an ArrayList of books and I want to delete a certain number of books only if they are different (using equals). How can I approach the problem?

For example, if I have 3 different books and my quantitytoremove is 3.. then those 3 books have to be deleted. Assuming quantitytoremove can't be higher than the number of DIFFERENT books inside the array.

I tried something like this but I know there are issues with what I wrote. Is there a function available with streams or else that can help me?


   public Basket removeDifferent(int quantitytoremove) {
        int removed=0;

        for (int i = 0; i < this.numberBooks && removed<quantitytoremove ; i++) {
            for (int j = i+1; j < this.numberBooks && removed<quantitytoremove; j++) {
                if (!(this.BooksArray.get(i).equals(this.BooksArray.get(j)))) {

                    Book Book_i= this.BooksArray.get(i);
                    Book Book_j= this.BooksArray.get(j);
                    this.BooksArray.remove(Book_i);
                    this.BooksArray.remove(Book_j);

                    removed=removed+2;
                    this.numberBooks=numberBooks-2;
                    i=0;j=1;

                }

            } 
        }
    }

解决方案

First, @Override equals() in class Foo to decide what's the attributes that make two objects equals and remove using recursion

Explain how recursion works

qn represents how many removes are done

Starting point i = 0 j = 1 and recursion till the base condition reached

Step 1, Base condition: if i or j exceeded the size of the array or qn exceeded the quantityToRemove return qn

Step 2, Check equality if array elements of i and j not equal then remove and increment qn and decrement j to avoid missing element

Step 3 for all i and increment j Repeat to Step 3 till reaching the base condition

Step 4 increment ++i

Step 5 start new recursion with increment i and start j from i+1

After the recussion has finished, check if qn not exceeded the quantityToRemove and remove from the array if contains 1 book. this for a case when you have different 4 books and want all of them to be removed

...

Trace

i = 0, j = 1, 2, ...., N

i = 1, j = 2, 3, ...., N

i = 2, j = 3, 4, ...., N

i = 3, j = 4, 5, ...., N

...

   static class Book {
        int id;
        String title;

        public Book(int id, String title) {
            this.id = id;
            this.title = title;
        }

        @Override
        public boolean equals(Object obj) {
            Book other = (Book) obj;
            return this.title.equals(other.title);
        }

        @Override
        public String toString() {
            return id + ": " + title;
        }
    }

    public static void main(String[] a) {
        Book b1 = new Book(1, "b1");
        Book b2 = new Book(2, "b2");
        Book b3 = new Book(3, "b3");
        Book b4 = new Book(4, "b4");
        Book b5 = new Book(1, "b1");
        List<Book> list = new ArrayList<>();
        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
        list.add(b5);
        list.add(new Book(1, "b1"));
        list.add(new Book(1, "b1"));
        list.add(new Book(1, "b1"));

        System.out.println(list);
        removeDifferent(list, 2);
        System.out.println(list);
    }

    static void removeDifferent(List<Book> booksArray, int quantityToRemove) {
        int qn = removeDifferent(booksArray, quantityToRemove, 0, 0, 1);
        if (booksArray.size() == 1 && qn < quantityToRemove)
            booksArray.remove(0);
    }

    static int removeDifferent(List<Book> booksArray, int quantityToRemove, int qn, int i, int j) {
        if (i >= booksArray.size() || j >= booksArray.size() || qn >= quantityToRemove)
            return qn;
        if (!booksArray.get(i).equals(booksArray.get(j))) {
            booksArray.remove(j);
            j--;
            qn++;
        }
        qn = removeDifferent(booksArray, quantityToRemove, qn, i, 1 + j);
        ++i;
        qn = removeDifferent(booksArray, quantityToRemove, qn, i, i + 1);
        return qn;
    }

, output

[1: b1, 2: b2, 3: b3, 4: b4, 1: b1, 1: b1, 1: b1, 1: b1]
[1: b1, 4: b4, 1: b1, 1: b1, 1: b1, 1: b1]

, And for this input

        Book b1 = new Book(1, "b1");
        Book b2 = new Book(2, "b2");
        Book b3 = new Book(3, "b3");
        Book b4 = new Book(4, "b4");
        List<Book> list = new ArrayList<>();
        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
        System.out.println(list);
        removeDifferent(list, 4);
        System.out.println(list);

, output

[]

这篇关于如何在Java中使用ArrayList移除不等于对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆