Array out of bounds in java but it worked in C -
this question has answer here:
hey guys getting error did same in c , worked when did in java getting error "exception in thread "main" java.lang.arrayindexoutofboundsexception: 5" can -->
import java.util.scanner; class sort { public static void main(string args[]) { scanner obj = new scanner(system.in); int a[] = new int[5]; int i, j; int temp; system.out.println("enter elements of array : "); (i = 0; < 5; i++) { a[i] = obj.nextint(); } (i = 0; < 5; i++) (j = 0; j < 5; j++) { if (a[i + 1] < a[i]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } (i = 0; < 5; i++) system.out.println("\n" + a[i]); } }
your problem here:
(i = 0; < 5; i++) (j = 0; j < 5; j++) { // when `i` == `4` accesses `a[5]` not exist. if (a[i + 1] < a[i]) {
Comments
Post a Comment