4

im trying to select randomly from an array to print it, then remove it from the array, to avoid printing out the same number twice. i am a bit of a java novice so was wondering if someone could point me where im going wrong.

public static void main(String[] args) {
    int[] colm = { 1, 2, 3, 4, 5, 67, 87 };
    Random rand = new Random();

    for (int i = 0; i < 5; i++)
        System.out.println(" " + colm[rand.nextInt(colm.length)]);

}

thanks

3
  • You don't want to print the same number twice,hence everytime you'll have to decrease the range of Random number created Commented Apr 4, 2014 at 9:55
  • Also, you are not removing the number from Array after you've picked it. You are just printing it on the console. Commented Apr 4, 2014 at 9:57
  • And since we don't live in caves, we don't wear human skins, please don't use Array. Cheers Commented Apr 4, 2014 at 9:58

5 Answers 5

5

Random doesn't give gurranty of unique number. you can do following instead.

public static void main(String[] args) {
    int[] colm = { 1, 2, 3, 4, 5, 67, 87 };
    List l = new ArrayList();
    for(int i: colm)
        l.add(i);

    Collections.shuffle(l);

    for (int i = 0; i < 5; i++)
        System.out.println(l.get(i));

}
Sign up to request clarification or add additional context in comments.

3 Comments

More elegant than others, but you didn't remove elements. Am I right?
@Gabrer. yes I didn't remove that. Because although the title asked to remove from array but the main goal of OP was to avoid printing duplicate entries.
Ok! I understand. But actually, I chose this answer on Google because in the title there was the "remove". It's up to you if improve the answer (anyway, it needs only to call the "remove()" method).
3

You are missing the remove part. Try something like this:

public static void main(String[] args)
{
    Integer [] colm = {1,2,3,4,5,67,87};
    final List<Integer> ints = new ArrayList<Integer>(Arrays.asList(colm));
    Random rand =  new Random();

    for(int i = 0; (i<5) && (ints.size() > 0); i ++) {
        final int randomIndex = rand.nextInt(ints.size());
        System.out.println(" " +  ints.get(randomIndex));
        ints.remove(randomIndex);
    }
}

2 Comments

it will throw exception if you are using list as final.
The exception was not due to final, Arrays.asList(colm) creates fixed size List which do not support removing, fixed that :)
0

You better use Set or Map to hold the data and then create random number which belongs in the length of the set/map and remove with that (random) index.

2 Comments

Set and Map don't have indices. A List would be more appropriate.
Also maybe the OP does this for exercise, so it would be great to give him an answer for his code.
0
public static void removeElements(int[] arr) {
        Random random = new Random();

        List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());

        int length = arr.length;
        while (length > 0) {

            System.out.println("Size: " + list.size());
            if (list.size() == 1) {
                int randomIndex = random.nextInt(list.size());
                list.remove(randomIndex);
                System.out.println("Size:--> " + list.size());
                break;
            }else {
                int randomIndex = random.nextInt(list.size() - 1);
                if (arr == null || randomIndex > arr.length) {
                    System.out.println("No Elements to be deleted");
                }
                list.remove(randomIndex);
                System.out.println("Removed Element: " + list.get(randomIndex));
                length--;

                if (length == 0)
                    break;

            }
        }

    }

Comments

0
public static void main(String[] args) {
int[] colm = { 1, 2, 3, 4, 5, 67, 87 };
List l = new ArrayList();
for(int i: colm)
    l.add(i);

Collections.shuffle(l);

for (int i = 0; i < 5; i++)
    System.out.println(l.get(i));

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.