0

Hello all I have the following line of code

solution first = mylist.remove((int)(Math.random() * mylist));

Which give me an error stating

The operator * is undefined for the argument type(s) double, ArrayList<solution>

I am trying to remove a random number in my arrayList from my ArrayList

Any help would be appriciated.

2
  • please include the declaration for mylist Commented Mar 4, 2013 at 18:15
  • @foampile From the error message, myList is declared as ArrayList<solution> Commented Mar 4, 2013 at 18:16

2 Answers 2

4

It looks like you are attempting to remove a random element from your list. To cover all elements with your random index, you need the list size.

It doesn't make sense to multiply a number by an ArrayList. You can't get the size of your list by directly specifying just your list in your code. Call the size() method on your list. That returns an int that can be multiplied.

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

Comments

0

You need to find a random number in within the range of your list size

final Random random = new Random();

mylist.remove(random.nextInt(myList.size()));

Make sure you create a Random and store it as otherwise it may create the same number repeatedly (it is only pseudorandom).

Also, the nextInt method excludes the top limit so mylist.size() will not return an invalid index.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.