Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
ArrayIndexOutOfBounds
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
I like...
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I wrote the following code to implement merge sort
public class Main { int[] array; int[] temp; int length; public static void main(String[] args)throws Exception { int[] inputArray = {45,23,11,89,77,98,4,28,65,43,12}; Main m = new Main(); m.sort(inputArray); for (int i : inputArray){ System.out.print(i+" "); } } public void sort(int[] inArr){ this.array=inArr; this.length =inArr.length; this.temp = new int[length]; doMerge(0,length-1); } public void doMerge(int low,int high){ if(low<high){ int middle = low + (high-low)/2; //Below step sorts the left side of the array doMerge(low,middle); //Below step sorts the right side of the array doMerge(middle+1,high); //Below part merges both sides mergeParts(low,middle,high); } } public void mergeParts(int low,int middle,int high){ for(int i = low;low<=high;i++){ temp[i]=array[i]; } int i = low; int j = middle; int k = low; while(i<=middle && j<= high){ if (temp[i]<=temp[j]){ array[k]=temp[i]; i++; } else{ array[k]=temp[j]; j++; } k++; } while(i<=middle){ array[k]=temp[i]; k++; i++; } } }
But it is giving me the following exception
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Main.mergeParts(Main.java:38) at Main.doMerge(Main.java:33) at Main.doMerge(Main.java:27) at Main.doMerge(Main.java:27) at Main.doMerge(Main.java:27) at Main.sort(Main.java:20) at Main.main(Main.java:11) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
I do not know why i am getting this and how to fix this.Can someone please help.
Liutauras Vilda
Marshal
Posts: 8857
637
I like...
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Rrohit,
Lets assume you have an array:
int[] A = {4};
You're getting ArrayIndexOutOfBoundsException when you're trying to access position within an array, which doesn't exist.
Example:
int i = 1; System.out.println(A[i]); // Array contains only 1 element, his index is 0 - Exception is thrown // But in this case trying to access element within position 1, which would be a 2nd element
Start debugging by trying put the print statements before the line, where exception is being thrown.
Example:
int i = 1; System.out.println(i); // gives output 1, so you can notice, that you're trying to access non existing indexed element. System.out.println(A[i]);
Jay Tee
Greenhorn
Posts: 1
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I would investigate this portion of your code:
for (int i = low; low <= high; i++) { temp[i] = array[i]; }
Campbell Ritchie
Marshal
Posts: 79180
377
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the Ranch
Good catch.
Rrohit rakesh upadhyay
Ranch Hand
Posts: 94
1
I like...
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank you Jay Tee, that fixed my code. Thanks for your advice Liutauras Vilda
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Creating a merge sort for string arrays,
mergesort C program using recursion
Why is my ShellSort so much faster than my QuickSort?
More...