Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Array Index Out of bounds?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Index Out of bounds?

    ]public static int[] encode(int array[]) throws FileNotCompressedException 
    	{
    	    int[] count=new int[array.length];
    		int[] Tarray=new int[array.length];
    		int j=0;
    		int c=0;
    		int NewArray[]=new int[array.length];
    		System.out.println(array[2]);
    		for (int i=0; i<array.length; i++)
    		{	
    			if(array[i]==array[i+1])
    			{
    				if(count[j]==0)
    				{
    					count[j]++;
    				}
    				count[j]++;
    			}
    			else
    			{
    				Tarray[c]=array[i];
    				c++;
    				j++;
     
    				if(count[j]==0)
    				{
    					count[j]++;
    				}
     
    			}
     
    		}
    		if(count.length+Tarray.length>=array.length)
    		{
     
    			throw new FileNotCompressedException();
    		}
    		else
    		{	int a=0;
    			int k=0;
    			while(a<NewArray.length)
    			{	
     
    				NewArray[a]=count[k];
    				NewArray[a+1]=Tarray[k];
    				a=a+2;
    				k++;
    			}
    			return NewArray;
    		}
     
     
    	}

    This is a small part of my code but to keep it short, i read from a file that has 1000's of int then i need to sort them in a special way but when i use my for loop to begin my sort i get that error on array[i]=array[i+1]; that says array index out of bounds but when i print array[1000] or any other it works fine;


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Array Index Out of bounds?

    Let's say that your array has 1000 cells which would mean it has indices 0-999. Your for loop would use i from 0-999, the problem is when you add 1 you would get 1000 which is not an index in the array hence the index out of bounds.

Similar Threads

  1. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  2. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  3. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM
  4. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM