简体   繁体   English

在2的Java循环中从2的ArrayList中删除元素

[英]Removing elements from 2 ArrayList in java inside 2 for loops

There are N boys and N girls. 有N个男孩和N个女孩。 Only a boy and a girl can form a dancing pair (ie no same sex dancing pairs are allowed). 只有一个男孩和一个女孩可以组成跳舞对(即,不允许同性跳舞对)。 The only other condition in making pairs is that their absolute difference in height should be less than or equal to K. 成对时的唯一其他条件是它们的绝对高度差应小于或等于K。

Find the maximum number of pairs that can be formed so that everyone has a unique partner. 找到可以形成的最大对数,以便每个人都有唯一的伙伴。

Input Format The first line will contain two integers, N and K. The second line will contain N integers, the heights of N boys. 输入格式第一行将包含两个整数N和K。第二行将包含N个整数,N个男孩的高度。 The third line will contain N integers, the heights of N girls. 第三行将包含N个整数,即N个女孩的高度。

Constraints 1≤N≤105 1≤K≤109 1≤height of boy or girl≤109 约束1≤N≤1051≤K≤1091≤男孩或女孩的身高≤109

Output Format A single line containing the maximum number of possible pairs. 输出格式包含最大可能对数的一行。

My problem is in deleting the elements from the tow arrays after satisfying the condition. 我的问题是在满足条件后从拖曳数组中删除元素。 (after storing the 2 arrays) (存储两个数组后)

The first algorithm i used : 我使用的第一个算法:

Arrays.sort(ArrBoys);
    Arrays.sort(ArrGirls);
    double count = 0;
    for (int i = 0; i < ArrGirls.length; i++) {
        for (int j = 0; j < ArrBoys.length; j++) {
            if ((int) Math.abs(ArrBoys[j] - ArrGirls[i]) <= k) {
                ArrBoys[j] = 0;
                ArrGirls[i] = 0;
                count++;
                break;
            }
        }
    }
    System.out.println((int) count);

and this is definitely wrong, because we need to delete that elements not to make them zeros, So i realized that i need to convert them to ArrayLists. 这绝对是错误的,因为我们需要删除该元素而不是使其为零,所以我意识到我需要将它们转换为ArrayLists。 then i came with this code : 然后我来了这个代码:

double count = 0;
    int dif = 0;
    for (Integer ArrGirl : ArrGirls) {
        loop1:
        {
            for (Integer ArrBoy : ArrBoys) {
                dif = (int) Math.abs(ArrBoy - ArrGirl);
                if (dif <= k) {
                    System.out.println("we took " + ArrBoy + " from boys with "
                    + ArrGirl + " from girls, thier dif is " + dif);
                    ArrBoys.remove(ArrBoy);
                    ArrGirls.remove(ArrGirl);
                    count++;
                    break loop1;
                }
            }
        }
    }
    System.out.println((int) count);

using this algorithm gives me an exception like this : 使用这种算法给我这样的异常:

Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) at java.util.ArrayList$Itr.next(ArrayList.java:851)

after i searched about this exception i found that i should use Iterator while using remove method, then i used this code : 在我搜索了此异常之后,我发现我应该在使用remove方法时使用Iterator,然后我使用了以下代码:

double count = 0;
    int dif = 0;
    Iterator<Integer> iteB = ArrGirls.iterator();
    Iterator<Integer> iteG = ArrGirls.iterator();
    while (iteG.hasNext()) {
        int value2 = iteG.next();
        while (iteB.hasNext()) {
            int value = iteB.next();
            dif = (int) Math.abs(value - value2);
            if (dif <= k) {
                System.out.println("we took " + value + " from boys with " + value2
                        + " from girls, thier dif is " + dif);
                count++;
                iteB.remove();
                iteG.remove();

            }
        }
    }
    System.out.println((int) count);

this code gives me wrong information and also gives me the same exception. 这段代码给了我错误的信息,也给了我同样的异常。

This is the first time i use ArrayList, do you have any idea to correct this code? 这是我第一次使用ArrayList,您是否有任何纠正此代码的想法?

You have to reassign the iterator for girls everytime.And this works fine 您每次都必须为女孩重新分配迭代器。

 int k = 5;
    int count = 0;
    ArrayList<Integer> ArrGirls = new ArrayList<>((List<Integer>) Arrays.asList(new Integer[]{34, 37, 28, 16, 44, 36, 37, 43, 50, 22}));
    ArrayList<Integer> ArrBoys = new ArrayList<>((List<Integer>) Arrays.asList(new Integer[]{13, 28, 41, 10, 14, 27, 41, 27, 23, 37}));
    for (Iterator<Integer> iteG = ArrGirls.iterator(); iteG.hasNext();) {
        {
            Integer ArrGirl = iteG.next();
            for (Iterator<Integer> iteB = ArrBoys.iterator(); iteB.hasNext();) {
                Integer ArrBoy = iteB.next();
                int dif = (int) Math.abs(ArrBoy - ArrGirl);
                if (dif <= k) {
                    System.out.println("we took " + ArrBoy + " from boys with "
                            + ArrGirl + " from girls, thier dif is " + dif);
                    ArrBoys.remove(ArrBoy);
                    ArrGirls.remove(ArrGirl);
                    iteG = ArrGirls.iterator();//You have to reset the value of iterationGirls
                    count++;
                    break;
                }
            }
        }
    }
    System.out.println(count);
// This helps I guess with little modifications for taking the inputs
public class Boy {

    private int height;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

}

public class Girl {

    private int height;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

}

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class Program {

    public static void main(String[] args) {
        int k=11;
        Random random = new Random();
        List<Boy> boys = new ArrayList<Boy>();
        List<Girl> girls = new ArrayList<Girl>();
        int noOfcombinations = 0;
        for(int i=1;i<=100;i++)
        {
            Boy boy = new Boy();
            boy.setHeight(random.nextInt(109));
            Girl girl = new Girl();
            girl.setHeight(random.nextInt(109));
            boys.add(boy);
            girls.add(girl);
        }
        for (Boy boy : boys) {
            for (Girl girl : girls) {
                if(Math.abs(boy.getHeight()-girl.getHeight())<k)
                {
                    noOfcombinations++;
                }

            }
        }
        System.out.println("No of combinations is "+noOfcombinations);

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM