• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

IndexOutOfBoundException

 
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?

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • 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 Windows XP Java
  • 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?
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
Ranch Hand
Posts: 859
IBM DB2 Chrome 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: 10777
71
Hibernate Eclipse IDE 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
IBM DB2 Chrome 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: 10777
71
Hibernate Eclipse IDE 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
 
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
 
Marshal
Posts: 71632
312
  • 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)
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic