Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException error in java [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
24 answers
I'm now writing a quicksort program in java and encountered an error. The terminal says that
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 101 out of bounds for length 19
at QuickSort.swap(qs.java:16)
at QuickSort.partition(qs.java:23)
at QuickSort.quickSort(qs.java:9)
at QuickSort.quickSort(qs.java:5)
at QuickSort.main(qs.java:40)
this is my first programming in java and this error did not occur when I tested this with an array that is a size of 10 and another array that has the same size to the array in the following program.
I did my research and this error occurs when the program access the wrong indexed array or out of range of loops but it works with other arrays that have the same size.
import java.util.Arrays;
public class QuickSort {
public void quickSort(int A) {
quickSort(A, 0, A.length - 1);
}
private void quickSort(int A, int low, int high) {
if (low < high + 1) {
int p = partition(A, low, high);
quickSort(A, low, p - 1);
quickSort(A, p + 1, high);
}
}
private void swap(int A, int index1, int index2) {
int temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
}
private int getPivot(int A, int low, int high) {
return A[low];
}
private int partition(int A, int low, int high) {
swap(A, low, getPivot(A, low, high));
int border = low + 1;
for (int i = border; i <= high; i++) {
if (A[i] < A[low]) {
swap(A, i, border++);
}
}
swap(A, low, border - 1);
return border - 1;
}
public static void main(String args) {
QuickSort qs = new QuickSort();
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
System.out.println(Arrays.toString(A));
long start = System.nanoTime();
qs.quickSort(A);
long end = System.nanoTime();
long elapsed_secs = end - start;
double seoconds = (double)elapsed_secs / 1_000_000_000.0;
System.out.println(Arrays.toString(A));
System.out.println(seoconds);
}
}
java indexoutofboundsexception
marked as duplicate by cricket_007
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 2:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
24 answers
I'm now writing a quicksort program in java and encountered an error. The terminal says that
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 101 out of bounds for length 19
at QuickSort.swap(qs.java:16)
at QuickSort.partition(qs.java:23)
at QuickSort.quickSort(qs.java:9)
at QuickSort.quickSort(qs.java:5)
at QuickSort.main(qs.java:40)
this is my first programming in java and this error did not occur when I tested this with an array that is a size of 10 and another array that has the same size to the array in the following program.
I did my research and this error occurs when the program access the wrong indexed array or out of range of loops but it works with other arrays that have the same size.
import java.util.Arrays;
public class QuickSort {
public void quickSort(int A) {
quickSort(A, 0, A.length - 1);
}
private void quickSort(int A, int low, int high) {
if (low < high + 1) {
int p = partition(A, low, high);
quickSort(A, low, p - 1);
quickSort(A, p + 1, high);
}
}
private void swap(int A, int index1, int index2) {
int temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
}
private int getPivot(int A, int low, int high) {
return A[low];
}
private int partition(int A, int low, int high) {
swap(A, low, getPivot(A, low, high));
int border = low + 1;
for (int i = border; i <= high; i++) {
if (A[i] < A[low]) {
swap(A, i, border++);
}
}
swap(A, low, border - 1);
return border - 1;
}
public static void main(String args) {
QuickSort qs = new QuickSort();
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
System.out.println(Arrays.toString(A));
long start = System.nanoTime();
qs.quickSort(A);
long end = System.nanoTime();
long elapsed_secs = end - start;
double seoconds = (double)elapsed_secs / 1_000_000_000.0;
System.out.println(Arrays.toString(A));
System.out.println(seoconds);
}
}
java indexoutofboundsexception
marked as duplicate by cricket_007
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 2:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
24 answers
I'm now writing a quicksort program in java and encountered an error. The terminal says that
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 101 out of bounds for length 19
at QuickSort.swap(qs.java:16)
at QuickSort.partition(qs.java:23)
at QuickSort.quickSort(qs.java:9)
at QuickSort.quickSort(qs.java:5)
at QuickSort.main(qs.java:40)
this is my first programming in java and this error did not occur when I tested this with an array that is a size of 10 and another array that has the same size to the array in the following program.
I did my research and this error occurs when the program access the wrong indexed array or out of range of loops but it works with other arrays that have the same size.
import java.util.Arrays;
public class QuickSort {
public void quickSort(int A) {
quickSort(A, 0, A.length - 1);
}
private void quickSort(int A, int low, int high) {
if (low < high + 1) {
int p = partition(A, low, high);
quickSort(A, low, p - 1);
quickSort(A, p + 1, high);
}
}
private void swap(int A, int index1, int index2) {
int temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
}
private int getPivot(int A, int low, int high) {
return A[low];
}
private int partition(int A, int low, int high) {
swap(A, low, getPivot(A, low, high));
int border = low + 1;
for (int i = border; i <= high; i++) {
if (A[i] < A[low]) {
swap(A, i, border++);
}
}
swap(A, low, border - 1);
return border - 1;
}
public static void main(String args) {
QuickSort qs = new QuickSort();
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
System.out.println(Arrays.toString(A));
long start = System.nanoTime();
qs.quickSort(A);
long end = System.nanoTime();
long elapsed_secs = end - start;
double seoconds = (double)elapsed_secs / 1_000_000_000.0;
System.out.println(Arrays.toString(A));
System.out.println(seoconds);
}
}
java indexoutofboundsexception
This question already has an answer here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
24 answers
I'm now writing a quicksort program in java and encountered an error. The terminal says that
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 101 out of bounds for length 19
at QuickSort.swap(qs.java:16)
at QuickSort.partition(qs.java:23)
at QuickSort.quickSort(qs.java:9)
at QuickSort.quickSort(qs.java:5)
at QuickSort.main(qs.java:40)
this is my first programming in java and this error did not occur when I tested this with an array that is a size of 10 and another array that has the same size to the array in the following program.
I did my research and this error occurs when the program access the wrong indexed array or out of range of loops but it works with other arrays that have the same size.
import java.util.Arrays;
public class QuickSort {
public void quickSort(int A) {
quickSort(A, 0, A.length - 1);
}
private void quickSort(int A, int low, int high) {
if (low < high + 1) {
int p = partition(A, low, high);
quickSort(A, low, p - 1);
quickSort(A, p + 1, high);
}
}
private void swap(int A, int index1, int index2) {
int temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
}
private int getPivot(int A, int low, int high) {
return A[low];
}
private int partition(int A, int low, int high) {
swap(A, low, getPivot(A, low, high));
int border = low + 1;
for (int i = border; i <= high; i++) {
if (A[i] < A[low]) {
swap(A, i, border++);
}
}
swap(A, low, border - 1);
return border - 1;
}
public static void main(String args) {
QuickSort qs = new QuickSort();
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
System.out.println(Arrays.toString(A));
long start = System.nanoTime();
qs.quickSort(A);
long end = System.nanoTime();
long elapsed_secs = end - start;
double seoconds = (double)elapsed_secs / 1_000_000_000.0;
System.out.println(Arrays.toString(A));
System.out.println(seoconds);
}
}
This question already has an answer here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
24 answers
java indexoutofboundsexception
java indexoutofboundsexception
edited Nov 27 '18 at 2:50
cricket_007
84.5k1147120
84.5k1147120
asked Nov 27 '18 at 1:20
A.LeeA.Lee
14
14
marked as duplicate by cricket_007
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 2:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by cricket_007
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 2:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Check this
private int getPivot(int A, int low, int high) {
return A[low];
}
With
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
At first time, low is 101 -> get value A at index 101, but size of A is 19 -> Exception
What do you want to do with getPivot ?
Just by comment
swap(A, low, getPivot(A, low, high));
Result :
[101, 103, 102, 107, 110, 116, 114, 118, 112, 111, 109, 104, 117, 100, 105, 115, 113, 106, 119]
[100, 101, 102, 103, 104, 105, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
the getPivot was originally a function that returns a pivot that pointing the middle value of the vectors, but I wanted to make it point the first value of each vector how should I edit it to prevent this error?
– A.Lee
Nov 27 '18 at 4:29
so just return low instead of return A[low] ?
– VietDD
Nov 27 '18 at 4:49
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Check this
private int getPivot(int A, int low, int high) {
return A[low];
}
With
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
At first time, low is 101 -> get value A at index 101, but size of A is 19 -> Exception
What do you want to do with getPivot ?
Just by comment
swap(A, low, getPivot(A, low, high));
Result :
[101, 103, 102, 107, 110, 116, 114, 118, 112, 111, 109, 104, 117, 100, 105, 115, 113, 106, 119]
[100, 101, 102, 103, 104, 105, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
the getPivot was originally a function that returns a pivot that pointing the middle value of the vectors, but I wanted to make it point the first value of each vector how should I edit it to prevent this error?
– A.Lee
Nov 27 '18 at 4:29
so just return low instead of return A[low] ?
– VietDD
Nov 27 '18 at 4:49
add a comment |
Check this
private int getPivot(int A, int low, int high) {
return A[low];
}
With
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
At first time, low is 101 -> get value A at index 101, but size of A is 19 -> Exception
What do you want to do with getPivot ?
Just by comment
swap(A, low, getPivot(A, low, high));
Result :
[101, 103, 102, 107, 110, 116, 114, 118, 112, 111, 109, 104, 117, 100, 105, 115, 113, 106, 119]
[100, 101, 102, 103, 104, 105, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
the getPivot was originally a function that returns a pivot that pointing the middle value of the vectors, but I wanted to make it point the first value of each vector how should I edit it to prevent this error?
– A.Lee
Nov 27 '18 at 4:29
so just return low instead of return A[low] ?
– VietDD
Nov 27 '18 at 4:49
add a comment |
Check this
private int getPivot(int A, int low, int high) {
return A[low];
}
With
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
At first time, low is 101 -> get value A at index 101, but size of A is 19 -> Exception
What do you want to do with getPivot ?
Just by comment
swap(A, low, getPivot(A, low, high));
Result :
[101, 103, 102, 107, 110, 116, 114, 118, 112, 111, 109, 104, 117, 100, 105, 115, 113, 106, 119]
[100, 101, 102, 103, 104, 105, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
Check this
private int getPivot(int A, int low, int high) {
return A[low];
}
With
int A = {101,103,102,107,110,116,114,118,112,111,109,104,117,100,105,115,113,106,119};
At first time, low is 101 -> get value A at index 101, but size of A is 19 -> Exception
What do you want to do with getPivot ?
Just by comment
swap(A, low, getPivot(A, low, high));
Result :
[101, 103, 102, 107, 110, 116, 114, 118, 112, 111, 109, 104, 117, 100, 105, 115, 113, 106, 119]
[100, 101, 102, 103, 104, 105, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
edited Nov 27 '18 at 2:44
answered Nov 27 '18 at 2:35
VietDDVietDD
36828
36828
the getPivot was originally a function that returns a pivot that pointing the middle value of the vectors, but I wanted to make it point the first value of each vector how should I edit it to prevent this error?
– A.Lee
Nov 27 '18 at 4:29
so just return low instead of return A[low] ?
– VietDD
Nov 27 '18 at 4:49
add a comment |
the getPivot was originally a function that returns a pivot that pointing the middle value of the vectors, but I wanted to make it point the first value of each vector how should I edit it to prevent this error?
– A.Lee
Nov 27 '18 at 4:29
so just return low instead of return A[low] ?
– VietDD
Nov 27 '18 at 4:49
the getPivot was originally a function that returns a pivot that pointing the middle value of the vectors, but I wanted to make it point the first value of each vector how should I edit it to prevent this error?
– A.Lee
Nov 27 '18 at 4:29
the getPivot was originally a function that returns a pivot that pointing the middle value of the vectors, but I wanted to make it point the first value of each vector how should I edit it to prevent this error?
– A.Lee
Nov 27 '18 at 4:29
so just return low instead of return A[low] ?
– VietDD
Nov 27 '18 at 4:49
so just return low instead of return A[low] ?
– VietDD
Nov 27 '18 at 4:49
add a comment |