Java 在数组中添加和删除元素:练习

Java 在数组中添加和删除元素:练习,java,arrays,Java,Arrays,我希望用数字1到10填充一个数组a,然后从该数组中获取一个随机数,将其添加到数组b中,并从数组a中删除该元素。我想知道最有效的方法。编辑:(该练习要求数组中没有重复的值,并且每次调用该方法时排列都是随机的。)以下是我迄今为止的方法: public int[] nextPermutation() { int capOne = 10; int capTwo = 10; int aSize = 0; int bSize = 0; int[] a = new

我希望用数字1到10填充一个数组a,然后从该数组中获取一个随机数,将其添加到数组b中,并从数组a中删除该元素。我想知道最有效的方法。编辑:(该练习要求数组中没有重复的值,并且每次调用该方法时排列都是随机的。)以下是我迄今为止的方法:

public int[] nextPermutation() {
    int capOne = 10;
    int capTwo = 10;
    int aSize = 0;
    int bSize = 0;


    int[] a = new int[capOne];
    int[] b = new int[capTwo];

    int upperBound = 11;
    Random generator = new Random();

    //fill initial array with 1 - 10
    for (int i = aSize; i < 10; i++) {
        a[i] = i + 1;
        //companion variable for sizing array
        aSize++;
    }

    //Create a random integer and add it to array b
    //Remove same integer from array a
    //Repeat and remove another random integer from the remaining integers in array a and    add it to b


        permuted = b;
        return permuted;
        }
public int[]nextpermutat(){
int capOne=10;
int-capTwo=10;
int aSize=0;
int-bSize=0;
int[]a=新int[capOne];
int[]b=新的int[capTwo];
int上限=11;
随机生成器=新随机();
//用1-10填充初始数组
for(int i=aSize;i<10;i++){
a[i]=i+1;
//调整数组大小的伴随变量
aSize++;
}
//创建一个随机整数并将其添加到数组b中
//从数组a中删除相同的整数
//重复并从数组a中的剩余整数中删除另一个随机整数,然后将其添加到b中
排列=b;
返回置换;
}

如果不是完全错误的话,我可能会以一种低效的方式处理这个问题。如果是这样,我相信你会毫不犹豫地告诉我。非常感谢您的帮助。

这是一个使用swap生成随机排列的程序。嗯,我不确定哪种方法可以产生更好的结果,但交换应该比向数组中添加/删除更快:

public int[] nextPermutation() {
    int cap = 10;
    int[] a = new int[cap];
    Random generator = new Random();

    //fill initial array with 1 - 10
    for (int i = 0; i < cap; i++) {
        a[i] = i + 1;
    }

    for (int i = 0; i < cap; i++) {
        int j = generator.nextInt(cap);
        int x = a[j];
        a[j] = a[i];
        a[i] = x;
    }

    // You can reduce the size of the output array:
    // int output[] = new int[5];
    // System.arraycopy(a, 0, output, 0, 5);
    // return output;

    return a;
}
public int[]nextpermutat(){
int cap=10;
int[]a=新的int[cap];
随机生成器=新随机();
//用1-10填充初始数组
对于(int i=0;i
这是一个使用swap生成随机排列的程序。嗯,我不确定哪种方法可以产生更好的结果,但交换应该比向数组中添加/删除更快:

public int[] nextPermutation() {
    int cap = 10;
    int[] a = new int[cap];
    Random generator = new Random();

    //fill initial array with 1 - 10
    for (int i = 0; i < cap; i++) {
        a[i] = i + 1;
    }

    for (int i = 0; i < cap; i++) {
        int j = generator.nextInt(cap);
        int x = a[j];
        a[j] = a[i];
        a[i] = x;
    }

    // You can reduce the size of the output array:
    // int output[] = new int[5];
    // System.arraycopy(a, 0, output, 0, 5);
    // return output;

    return a;
}
public int[]nextpermutat(){
int cap=10;
int[]a=新的int[cap];
随机生成器=新随机();
//用1-10填充初始数组
对于(int i=0;i
您可以:

//randomly choose element
int index = (int) (Math.random() * aSize);
int dataFromA = a[index];

//"remove" it from A
aSize--;
for(int i = index; i<aSize; i++) {
    a[i] = a[i+1];
}

//"add" it to b
b[bSize] = dataFromA;
bSize++;
//随机选择元素
int index=(int)(Math.random()*aSize);
int dataFromA=a[index];
//将其从一个文件中“删除”
亚洲--;
对于(int i=索引;i您可以:

//randomly choose element
int index = (int) (Math.random() * aSize);
int dataFromA = a[index];

//"remove" it from A
aSize--;
for(int i = index; i<aSize; i++) {
    a[i] = a[i+1];
}

//"add" it to b
b[bSize] = dataFromA;
bSize++;
//随机选择元素
int index=(int)(Math.random()*aSize);
int dataFromA=a[index];
//将其从一个文件中“删除”
亚洲--;

对于(int i=index;i)您最好使用一个列表鉴于数组包含十个元素,我怀疑您是否需要过分担心效率。请关注正确性。Hi StinePike.As在List List=Arrays.asList()中或者一个ArrayList?你能有重复的值吗?不,我不能有重复的值。我应该指定它。我很抱歉。你最好使用一个列表鉴于数组包含十个元素,我怀疑你是否需要过分担心效率。关注正确性。Hi StinePike.As in List=Arrays.asList()或者一个ArrayList?你能有重复的值吗?不,我不能有重复的值。我应该指定它。我道歉。