简体   繁体   中英

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. Now I want to search the ArrayList for the same numbers, than remove them and get a new number. In the end, the list should only contain 50 unique numbers between 1 and 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. 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:

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.

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:

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. On the other hand using a LinkedList would require an iteration to reach the index of the element that should be removed.

You could use a linq query for this:

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..

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+ ", ");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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