• Post Reply Bookmark Topic Watch Topic
  • New Topic

removing random numbers from ArrayList?  RSS feed

 
Vee Cafelle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm supposed to loop 10 times and randomly remove one element from the ArrayList (list1) each time, while printing out the value that was removed and the remaining values in the list (all on one line). And I have to select a random number that is always strictly less than the number of elements left in the list.

Here is all of my code:



But I need help with this part:



I am trying to remove a random number from the list, but I'm removing all the numbers instead, so at the end I have an empty list and I just want to remove random numbers and have some elements in the list.

I'm not sure what to do next. I tried reading other threads but didn't have much luck. Could anyone help me out? I would truly appreciate it.

Vee
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it looks to me like you are calling: list1.remove(ran_num) ;

10 times so it seems logical that your list will be empty after that.

Just remove the: for (int j = 0 ; j < 10 ; j++)

to remove 1 element.

Use: for (int j = 0 ; j < 2 ; j++)

to remove 2 elements
etc.

I can't find anything wrong with your code at first glance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vee,
Welcome to CodeRanch!

You've stumbled upon a tricky case in Java. There are two method signatures for remove. The first one takes an int and removes the element at that index. This is what is going on in your code.You always remove a value because it is going by index and there is always a match. Since there are 10 random numbers and you always delete one each time, there are none at the end. For example:



The other takes an Integer and removes the element with that value. In this case, it only removes if there is a match. In this case there is no match:



The reason this is tricky is that int "autoboxes" (automatically converts) to Integer so you don't have to think about it. When I added elements to list, autoboxing was fine. However, when calling remove it is not. Java says "there is a signature with int (remove by index)" and it stops looking.
 
Vee Cafelle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:Well it looks to me like you are calling: list1.remove(ran_num) ;

10 times so it seems logical that your list will be empty after that.

Just remove the: for (int j = 0 ; j < 10 ; j++)

to remove 1 element.

Use: for (int j = 0 ; j < 2 ; j++)

to remove 2 elements
etc.

I can't find anything wrong with your code at first glance.


but I am asked to loop 10 times over the list, so I don't think I can make j < 2 because I wouldn't be iterating 10 times
 
Vee Cafelle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Vee,
Welcome to CodeRanch!

You've stumbled upon a tricky case in Java. There are two method signatures for remove. The first one takes an int and removes the element at that index. This is what is going on in your code.You always remove a value because it is going by index and there is always a match. Since there are 10 random numbers and you always delete one each time, there are none at the end. For example:



The other takes an Integer and removes the element with that value. In this case, it only removes if there is a match. In this case there is no match:



The reason this is tricky is that int "autoboxes" (automatically converts) to Integer so you don't have to think about it. When I added elements to list, autoboxing was fine. However, when calling remove it is not. Java says "there is a signature with int (remove by index)" and it stops looking.


Thanks for the welcome and for telling me about that!

However, I'm still not sure how to remove random numbers from the list while iterating?
Should I be using a some sort of while condition or something?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, not sure but you might be looking for:



But it doesn't guarantees a number will be removed. Running it several times you will eventually see a number being removed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!