Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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 - Fatal编程技术网

如何移除阵列中的随机对象?java新手

如何移除阵列中的随机对象?java新手,java,Java,我正在做一个小项目,在一个包数组中有两种不同类型的球对象,我想从这个包中随机抽取两个对象。我遇到的问题是从包阵列中移除对象。我成功地获得了两个随机对象,但它在袋阵列中占据了它们的位置,我真的不知道如何从袋阵列中移除对象 public class Bag { public static void main(String[] args) { Balls whiteBalls = new Balls("White"); Balls blac

我正在做一个小项目,在一个包数组中有两种不同类型的球对象,我想从这个包中随机抽取两个对象。我遇到的问题是从包阵列中移除对象。我成功地获得了两个随机对象,但它在袋阵列中占据了它们的位置,我真的不知道如何从袋阵列中移除对象

public class Bag {

    public static void main(String[] args) {
        Balls whiteBalls = new Balls("White");
        Balls blackBalls = new Balls("Black");
        Balls[] objArray;
        whiteBalls.setAmount(16);
        blackBalls.setAmount(20);
        int totalBalls = whiteBalls.getAmount() + blackBalls.getAmount();
        // Create two arrays that hold white and black balls together.
        Balls[] white = new Balls[whiteBalls.getAmount()];
        Balls[] black = new Balls[blackBalls.getAmount()];
        // Adding white balls into array "white".
        for (int i = 0; i < whiteBalls.getAmount(); i++) {
            white[i] = new Balls("White");
        } // Adding black balls into array "black"
        for (int i = 0; i < blackBalls.getAmount(); i++) {
            black[i] = new Balls("Black");
        }
        Balls[] bag = new Balls[totalBalls]; // Making a bag which holds all the balls.
        // Copy's both arrays and fills up array "bag".
        System.arraycopy(white, 0, bag, 0, whiteBalls.getAmount());
        System.arraycopy(black, 0, bag, 16, blackBalls.getAmount());
        //System.out.println(Arrays.toString(bag));

        int count = 0;
        Random rnd = new Random();
        String[] colours = new String[]{"White", "Black"};

        while(bag.length != 1){ // Depending on what colour the balls are either black ball or       white ball is placed back into the bag.
            count++;
            int select1 = rnd.nextInt(colours.length);
            int select2 = rnd.nextInt(colours.length);
            while(white.length != 0 || black.length != 0){
                if(select1 == 0 && select2 == 0){
                    System.out.println("Both are the same colour. " + select1 + " " + select2);

                }
            }
        }
    }
}

任何反馈都将不胜感激。感谢您的阅读并提前表示感谢。

我建议您使用ArrayList,因为它提供了从列表中删除元素的方法(请注意,在大型数组中这可能会非常昂贵)。remove()方法可以获取要删除的对象的索引,也可以获取要删除的对象本身的索引,因为该方法已重载。请注意,如果存储到保存包中应删除元素索引的int,可能会导致意外结果,因为在存储索引后删除对象后,此索引指向错误的对象。

因此,我希望此答案对您有所帮助。

感谢您的回答。很抱歉再次打扰你,但我遇到了另一个问题。我将数组转换为ArrayList一切正常,现在我需要检查random方法使用的对象,并与其他方法进行比较。我不知道怎么做,因为随机方法是拉索引,我不知道如何比较它们,因为我设置了它们的类型来表示每个对象的颜色<代码>如果(select1=“Black”&&select2=“Black”){我正试图在while循环内部执行此操作。遗憾的是,我不知道如何为您粘贴该代码。希望您有时间与我联系。因此,我将使用get()从您的数组列表中获取对象实例。然后,如果要删除某个项目,可以使用该实例。如果要比较项目的两种颜色,我不建议使用select1==“Black”,因为它不起作用。您应该使用select1.equals(“Black”),因为它比较字符串的内容,而不是变量的标识。
public class Balls {
    private String colour;
    private int amount;

    public Balls(String type) {
        colour = type;
    }

    public String getColour() {
        return colour;

    }

    public void changeAmount(int num){
        this.setAmount(num);
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String toString() {
        return colour;

    }
}