Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 生成随机数对,不重复_Java_Arrays_Random_Combinations - Fatal编程技术网

Java 生成随机数对,不重复

Java 生成随机数对,不重复,java,arrays,random,combinations,Java,Arrays,Random,Combinations,我必须使用整数数组: int[] a={1,2,3,4,5}; int[] b={6,7}; 我想生成一个数组,它包含来自a和b数组的对,以随机顺序,没有重复项。例如,我希望得到以下结果: c={(1,6),(2,7),(4,6),...} 谢谢 这里有一些代码,它从您的输入a[]和b[]数组中创建10个随机对,并将它们存储到哈希集中,您可以在以后使用它。HashSet将自动删除重复对 public class Pair { int x; int y; public

我必须使用整数数组:

int[] a={1,2,3,4,5};
int[] b={6,7};
我想生成一个数组,它包含来自a和b数组的对,以随机顺序,没有重复项。例如,我希望得到以下结果:

c={(1,6),(2,7),(4,6),...}

谢谢

这里有一些代码,它从您的输入
a[]
b[]
数组中创建10个随机对,并将它们存储到
哈希集中,您可以在以后使用它。
HashSet
将自动删除重复对

public class Pair {
    int x;
    int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Pair otherPair = (Pair) obj;
        if (this.getX() != otherPair.getX() || this.getY() != otherPair.getY()) {
            return false;
        }

        return true;
    }

    // getters and setters
}

public class PairTest {
    public static void main(String[] args) {
        Random randomGenerator = new Random();

        int[] a={1,2,3,4,5};
        int[] b={6,7};

        Set<Pair> pairs = new HashSet<Pair>();

        do {
            int xRand = randomGenerator.nextInt(a.length);
            int yRand = randomGenerator.nextInt(b.length);

            Pair p;
            if (xRand % 2 == 0) {
                Pair p = new Pair(a[xRand], b[yRand]);
            }
            else {
                Pair p = new Pair(b[yRand], a[xRand]);
            }
            pairs.add(p);
            if (pairs.size() == 10) break;
        } while (true);
    }
}
公共类对{
int x;
int-y;
公共对(整数x,整数y){
这个.x=x;
这个。y=y;
}
@凌驾
公共布尔等于(对象obj){
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
最后一对otherPair=(对)obj;
如果(this.getX()!=otherPair.getX()| | this.getY()!=otherPair.getY()){
返回false;
}
返回true;
}
//接球手和接球手
}
公共类配对测试{
公共静态void main(字符串[]args){
Random randomGenerator=新的Random();
int[]a={1,2,3,4,5};
int[]b={6,7};
Set pairs=新的HashSet();
做{
int xRand=randomGenerator.nextInt(a.length);
int yRand=randomGenerator.nextInt(b.长度);
对p;
如果(随机数%2==0){
对p=新对(a[xRand],b[yRand]);
}
否则{
对p=新对(b[yRand],a[xRand]);
}
配对。添加(p);
如果(pairs.size()==10)中断;
}虽然(正确);
}
}

以下是一些代码,它从您的输入
a[]
b[]
数组中创建10个随机对,并将它们存储到
哈希集中,您可以在以后使用它。
HashSet
将自动删除重复对

public class Pair {
    int x;
    int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Pair otherPair = (Pair) obj;
        if (this.getX() != otherPair.getX() || this.getY() != otherPair.getY()) {
            return false;
        }

        return true;
    }

    // getters and setters
}

public class PairTest {
    public static void main(String[] args) {
        Random randomGenerator = new Random();

        int[] a={1,2,3,4,5};
        int[] b={6,7};

        Set<Pair> pairs = new HashSet<Pair>();

        do {
            int xRand = randomGenerator.nextInt(a.length);
            int yRand = randomGenerator.nextInt(b.length);

            Pair p;
            if (xRand % 2 == 0) {
                Pair p = new Pair(a[xRand], b[yRand]);
            }
            else {
                Pair p = new Pair(b[yRand], a[xRand]);
            }
            pairs.add(p);
            if (pairs.size() == 10) break;
        } while (true);
    }
}
公共类对{
int x;
int-y;
公共对(整数x,整数y){
这个.x=x;
这个。y=y;
}
@凌驾
公共布尔等于(对象obj){
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
最后一对otherPair=(对)obj;
如果(this.getX()!=otherPair.getX()| | this.getY()!=otherPair.getY()){
返回false;
}
返回true;
}
//接球手和接球手
}
公共类配对测试{
公共静态void main(字符串[]args){
Random randomGenerator=新的Random();
int[]a={1,2,3,4,5};
int[]b={6,7};
Set pairs=新的HashSet();
做{
int xRand=randomGenerator.nextInt(a.length);
int yRand=randomGenerator.nextInt(b.长度);
对p;
如果(随机数%2==0){
对p=新对(a[xRand],b[yRand]);
}
否则{
对p=新对(b[yRand],a[xRand]);
}
配对。添加(p);
如果(pairs.size()==10)中断;
}虽然(正确);
}
}

我想像这样的东西就可以了。我在Pair类和main的末尾添加了一个toString,以便您可以看到输出

import java.util.ArrayList;
import java.util.Collections;

class Pair {

    private Integer a;
    private Integer b;

    public Pair(Integer a, Integer b) {
        super();
        this.a = a;
        this.b = b;
    }

    public Integer getA() {
        return a;
    }
    public void setA(Integer a) {
        this.a = a;
    }
    public Integer getB() {
        return b;
    }
    public void setB(Integer b) {
        this.b = b;
    }

    @Override
    public String toString() {
        return "Pair [a=" + a + ", b=" + b + "]";
    }


}

public class MainTest {

    public static void main(String[] args) {
        ArrayList<Pair> pairs = new ArrayList<Pair>();

        int[] a={1,2,3,4,5};
        int[] b={6,7};

        for (int i = 0; i<a.length; i++) {
            pairs.add(new Pair(a[i], b[(int) Math.round(Math.random())]));
        }
        Collections.shuffle(pairs);
        System.out.println(pairs);

    }

}
import java.util.ArrayList;
导入java.util.Collections;
类对{
私有整数a;
私有整数b;
公共对(整数a、整数b){
超级();
这个a=a;
这个.b=b;
}
公共整数getA(){
返回a;
}
公共空集a(整数a){
这个a=a;
}
公共整数getB(){
返回b;
}
公共void setB(整数b){
这个.b=b;
}
@凌驾
公共字符串toString(){
返回“Pair[a=“+a+”,b=“+b+””;
}
}
公共类测试{
公共静态void main(字符串[]args){
ArrayList pairs=新的ArrayList();
int[]a={1,2,3,4,5};
int[]b={6,7};

对于(inti=0;i我想类似的东西就可以了。我在Pair类和main的末尾添加了一个toString,这样您就可以看到输出了

import java.util.ArrayList;
import java.util.Collections;

class Pair {

    private Integer a;
    private Integer b;

    public Pair(Integer a, Integer b) {
        super();
        this.a = a;
        this.b = b;
    }

    public Integer getA() {
        return a;
    }
    public void setA(Integer a) {
        this.a = a;
    }
    public Integer getB() {
        return b;
    }
    public void setB(Integer b) {
        this.b = b;
    }

    @Override
    public String toString() {
        return "Pair [a=" + a + ", b=" + b + "]";
    }


}

public class MainTest {

    public static void main(String[] args) {
        ArrayList<Pair> pairs = new ArrayList<Pair>();

        int[] a={1,2,3,4,5};
        int[] b={6,7};

        for (int i = 0; i<a.length; i++) {
            pairs.add(new Pair(a[i], b[(int) Math.round(Math.random())]));
        }
        Collections.shuffle(pairs);
        System.out.println(pairs);

    }

}
import java.util.ArrayList;
导入java.util.Collections;
类对{
私有整数a;
私有整数b;
公共对(整数a、整数b){
超级();
这个a=a;
这个.b=b;
}
公共整数getA(){
返回a;
}
公共空集a(整数a){
这个a=a;
}
公共整数getB(){
返回b;
}
公共void setB(整数b){
这个.b=b;
}
@凌驾
公共字符串toString(){
返回“Pair[a=“+a+”,b=“+b+””;
}
}
公共类测试{
公共静态void main(字符串[]args){
ArrayList pairs=新的ArrayList();
int[]a={1,2,3,4,5};
int[]b={6,7};

对于(int i=0;i如果一个随机数组对您来说足够好,这是最简单的方法:只需创建所有可能的对并对结果进行洗牌,只要
a
b
包含唯一值,这将增加随机性和唯一性

public static void main(String[] args) {
    int[] a = { 1, 2, 3, 4, 5 };
    int[] b = { 6, 7 };

    ArrayList<Pair> pairs = new ArrayList<>();
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < b.length; j++) {
            pairs.add(new Pair(a[i], b[j]));
        }
    }

    Collections.shuffle(pairs);

    Pair[] asArray = pairs.toArray(new Pair[0]); //if you prefer array over ArrayList
}

class Pair {
    int a, b;

    public Pair(int a, int b) {
        this.a = a;
        this.b = b;
    }
}
publicstaticvoidmain(字符串[]args){
int[]a={1,2,3,4,5};
int[]b={6,7};
ArrayList pairs=新的ArrayList();
for(int i=0;i

如果您希望生成的对数少于总对数,那么有更有效的方法来生成所有对,然后只取前几个。如果是这样,请写下评论。

如果随机数组对您来说足够好,这
    int[] a={1,2,3,4,5};
    int[] b={6,7};
    List<int[]> list = new ArrayList<>();
    for (int i = 0; i < a.length; ++i)
        for (int j = 0; j < b.length; ++j)
            list.add(new int[] {a[i], b[j]});
    Collections.shuffle(list);