从2 for for循环

时间:2015-06-24 19:28:42

标签: java arrays algorithm arraylist iterator

有N个男孩和N个女孩。只有一个男孩和一个女孩可以形成一对舞蹈(即不允许同性别的舞蹈对)。制作配对的唯一其他条件是它们的高度绝对差应小于或等于K.

找出可以形成的最大对数,以便每个人都有一个独特的合作伙伴。

输入格式 第一行将包含两个整数,N和K. 第二行将包含N个整数,N个男孩的高度。 第三行将包含N个整数,即N个女孩的高度。

约束 1≤N≤105 1≤K≤109 1≤男孩或女孩的高度≤109

输出格式 包含最大可能对数的单行。

我的问题是在满足条件后删除两个数组中的元素。 (存储2个数组后)

我使用的第一个算法:

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);

这绝对是错误的,因为我们需要删除那些元素而不是让它们为零,所以我意识到我需要将它们转换为ArrayLists。 然后我带来了这段代码:

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);

使用这个算法给我一个这样的例外:

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)

在我搜索了这个异常之后,我发现我应该在使用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);

这段代码给了我错误的信息,也给了我同样的例外。

这是我第一次使用ArrayList,您是否有任何想法更正此代码?

2 个答案:

答案 0 :(得分:0)

你必须每次为女孩重新分配迭代器。这很好用

 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);

答案 1 :(得分:-1)

// 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);

    }

}