简体   繁体   English

如何在ArrayList中搜索相同的元素?

[英]how to search for the same element in an ArrayList?

This Code creates 50 random-numbers between 1 and 100 and adds it in an ArrayList. 此代码创建50个1到100之间的随机数,并将其添加到ArrayList中。 Now I want to search the ArrayList for the same numbers, than remove them and get a new number. 现在我想在ArrayList中搜索相同的数字,而不是删除它们并获得一个新数字。 In the end, the list should only contain 50 unique numbers between 1 and 100. 最后,列表应该只包含50到1到100之间的唯一数字。

The Problem is: I don't know, how to search the same ArrayList for the same number, remove it and get a new one. 问题是:我不知道,如何在相同的ArrayList中搜索相同的数字,删除它并获得一个新的。 Can someone please help me? 有人可以帮帮我吗?

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

class RandomPrim {

    public static void main(String[] args) {

        Random nr = new Random();
        int number;
        ArrayList<Integer> liste = new ArrayList<Integer>();

        // get 50 random numbers between 1 and 100
        for(int counter = 1; counter <= 50; counter++) {
            number = 1+nr.nextInt(100);
            liste.add(number);

            // System.out.println(liste.toString());
        }

        for (int ausgabe : liste) {
            System.out.print(ausgabe+ ", ");
        }
    }
}
Random nr = new Random();
int number;
ArrayList<Integer> liste = new ArrayList<Integer>();

// get 50 random numbers between 1 and 100
for(int counter = 1; counter <= 50; ) {
    number = 1+nr.nextInt(100);
    if(!(liste.contains(number))) {
        liste.add(number);
        counter++;
    }
}

for (int ausgabe : liste) {
    System.out.print(ausgabe+ ", ");
}

hope this helps. 希望这可以帮助。

Better use HashSet instead, to avoid duplications: 最好更好地使用HashSet ,以避免重复:

Random nr = new Random();
int number;

Set<Integer> randomSet = new HashSet<>();

// get 50 random numbers between 1 and 100
while(randomSet.size() < 50) {
    number = 1 + nr.nextInt(100);
    randomSet.add(number);
}

for (int ausgabe : randomSet) {
    System.out.print(ausgabe + ", ");
}

As already has been suggested using a Set or, if insertion order is important, a LinkedHashSet and keep generating random numbers until the set has a size of 50. 正如已经建议使用Set或者,如果插入顺序很重要,则使用LinkedHashSet并保持生成随机数,直到该集合的大小为50。

The problem with that, however, might be that as the set fills up you could get more and more duplicates thus requiring a lot of retries. 然而,问题可能在于,当设置填满时,您可能会获得越来越多的重复,因此需要进行大量重试。

So an alternative could be to use a list of 100 numbers and then randomly take one out: 所以另一种方法是使用100个数字的列表然后随机取出一个:

List<Integer> availableNumbers = new ArrayList<>( 100 );
for( int i = 1; i <= 100; i++ ) {
  availableNumbers.add( i );
}

Random r = new Random();
List<Integer> randomizedList = new ArrayList<>( 50 );
for( int i = 0; i < 50; i++ ) {
  int randomIndex = r.nextInt( availableNumbers.size() );
  randomizedList.add( availableNumbers.remove( randomIndex ) );
}

Note that using an ArrayList has the drawback that if you take out a number all numbers following it would have to be shifted to the left. 请注意,使用ArrayList的缺点是,如果取出一个数字,则跟随它的所有数字都必须向左移动。 On the other hand using a LinkedList would require an iteration to reach the index of the element that should be removed. 另一方面,使用LinkedList需要迭代才能到达应该删除的元素的索引。

You could use a linq query for this: 您可以使用linq查询:

liste.Distinct().ToArray();

This should at least get you headed in the right direction. 这至少应该让你朝着正确的方向前进。

If you don't want to try any of the other solutions, you can just get unique numbers from the beginning while still using an ArrayList like so.. 如果你不想尝试任何其他解决方案,你可以从头开始获取唯一的数字,同时仍然使用像这样的ArrayList。

Random nr = new Random();
int number;
ArrayList<Integer> liste = new ArrayList<Integer>();

// get 50 random numbers between 1 and 100
for(int counter = 1; counter <= 50; ) {
    number = 1+nr.nextInt(100);
    if(!(liste.contains(number))) {
        liste.add(number);
        counter++;
    }
    else {
        counter = counter - 1;
    }
}

for (int ausgabe : liste) {
    System.out.print(ausgabe+ ", ");
}

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

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