I Was solving School level problem "Find largest item in an array"
Here is the code
package geeksforgeeks;
import java.util.Scanner;
public
class LargestElementInArray {
public static void main(String[] args) {
int temp;
//varible used for sorting
int tempJ;
//varible used for swapping
int a[] = {5,5,2,3,5,2,6,23,25,23,5,355,333,3,15,15,25};
//array of whcih we will find the largest element
// array.length does not give the index value it just gives the normal counting so i subtracted
// 1 from the length to get the index length
int arrayLength = a.length - 1;
System.out.println(arrayLength);
//sorting starts
for(int i = 0; i < arrayLength; i++){
for(int j = 0; j < arrayLength ; j++){
tempJ = j + 1;
System.out.println(tempJ);
if(a[j] > a[tempJ])
//if the right element is smaller than it would swap their places using their index
// i don't think i need to provide how it is working it is really basic code
{
temp = a[j];
a[j] = a[i++];
a[i++] = temp;
}
}
}
//Printing the largest item present at the end of the array
System.out.println(a[arrayLength);
}
}
i sorted the array and printed the largest element which would at last of the array after sorting
Now Comes the error that i don't khow why is I am getting
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 17 out of bounds for length 17
at geeksforgeeks/geeksforgeeks.LargestElementInArray.main(LargestElementInArray.java:32)
Please Anser below why I am getting this error Thanks in advance