-1

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

name not found
  • 602
  • 4
  • 12
  • 2
    `tempJ = j + 1;` will get out of bounds if `j` runs all the way to the last index of the array (which in your loop it does). – Thilo Apr 23 '20 at 10:16
  • 1
    *Index 17 out of bounds for length 17* means you are trying to access index 17 but the last existing one is 16 (0 based indexes in arrays). You might want to check if the index actually exists for the case where you add any amount to index based loop variables. – deHaar Apr 23 '20 at 10:20
  • 1
    you are using both length -1 and < in the loops on the other hand a[tempj] in if is going out of bounds . Similar questions are already asked before here https://stackoverflow.com/a/41648461/10976207 . Please refer to them and don't duplicate the questions – Ashish Kumar Apr 23 '20 at 10:23

1 Answers1

2

Your code is not handling the range of the indexes properly. No need to keep a variable tempj for swapping. We can handle that in the logic.

This will Sort out your problem and give the largest number.

public static void main(String[] args) {
    int a[] = {5,5,2,3,5,2,6,23,25,23,5,355,333,3,15,15,25};
    int arrayLength = a.length;
    int temp;

    for (int i = 0; i < arrayLength; i++) {
        for (int j = i+1; j < (arrayLength - i); j++) {
            if (a[j - 1] > a[j]) {
                temp = a[j - 1];
                a[j - 1] = a[j];
                a[j] = temp;
            }
        }
    }
    //Printing the largest item present at the end of the array
    System.out.println(a[arrayLength-1]);
}
Sarangan
  • 803
  • 7
  • 20