Results 1 to 4 of 4
  1. #1
    np2392 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000
    	at SortersTester.main(SortersTester.java:10)
    Not sure why I'm getting this error. I'm trying to create arrays that gradually go up in size (start at 1,000 and increase by 1,000 each time until the last array created is 50,000) and fill them with random numbers between 1-1,000,000. Please help!

    Java Code:
    import java.util.Random;
    public class SortersTester {
    	public static void main (String[] args){
    		Random r = new Random();
    		for (int size=1000; size<=50000; size += 1000) {
    			int [] anArray = new int[size];
    			for (int i=0; i<=size; i++) {
    				int randint = r.nextInt(1000000);
    				anArray[i] = randint;
    			}		
    
    		}
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    Hint: If an array has a size of 10, what will its last index be? Draw it out on paper.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    np2392 is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Ah. So this:

    Java Code:
    for (int i=0; i<=size; i++) {
    Should be this:

    Java Code:
    for (int i=0; i<size; i++) {
    Correct? If so, thank you very much!

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. ArrayIndexOutOfBoundsException: 10 ??
    By Yakg in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 06:56 PM
  2. ArrayIndexOutOfBoundsException
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 06:50 PM
  3. ArrayIndexOutOfBoundsException
    By Corey in forum New To Java
    Replies: 5
    Last Post: 02-02-2010, 01:25 AM
  4. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM
  5. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 09:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •