• Post Reply Bookmark Topic Watch Topic
  • New Topic

IndexOutOfBoundException  RSS feed

 
Raihan Jamal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my below code in which I am trying to generate random unique number between 0 and 1001 and pass that number to some method. But this code gives me index out of bound exception. I am not sure why it is giving me that exception. Can anyone suggest me why is it happening?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
endRange condition in your 2nd for loop is ok?
 
Raihan Jamal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think so it is ok. As the List contains element starting from 1 to 1000 so I can retrieve the element from 1000 index size right?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List store 1 at 0 index and 1000 at 999. by the way: why cant you use Iterator or condition check on list size?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raihan Jamal wrote:I think so it is ok. As the List contains element starting from 1 to 1000 so I can retrieve the element from 1000 index size right?

No. A List (or array, or almost any collection) in Java of size 1,000 has elements 0-999.

Maybe you need to re-read the Java Tutorials.

Winston
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's going from 0 to 999 (which is 1000 elements)

WP
 
Raihan Jamal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Now it makes sense, So code should be something like the below after making changes If I need to generate unique random number between 0 and 1001?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raihan Jamal wrote:Ok. Now it makes sense, So code should be something like the below after making changes If I need to generate unique random number between 0 and 1001?

No, because the limit in your code is 1000 (the rest, however, looks fine; if a bit tortuous).

What exactly do you want to do?
Select ONE random number between 0/1-1000/1001; or get a list of one thousand randomly-selected unique numbers between 1-1000?

If the last, your code looks quite close.

You might also want to look at the java.util.Random class.

Winston
 
Raihan Jamal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want unique random number between 0 and 1001. But by using Random it will not be more efficient in this case I guess.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want from 1-1000 ?

WP
 
Raihan Jamal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. That is right.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raihan Jamal wrote:Yes. That is right.

Then, if you want one (or a few) random numbers, and you don't care if they're unique, use Random,nextInt(int).

Winston
 
Antonio Principato
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wanted it to be unique couldn't you just generate a random number using the java.util.Random class nested in a do while loop (until a boolean turns true meaning the new number was added) Then every time you generate a new random number you run through the array using a for loop checking each index to see if it matches the randomly generated number. If not add it to the array and turn the boolean true.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are different ways from running through an array. You can add the numbers to a Set<Integer> and reject any duplicates, using the return value of the add method.

But those numbers are not called random. Randomness implies the possibility of repetition. That is called random selection from a declining population (or similar)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!