Fixing ArrayOutOfBound Error in BinarySearch

  • Context: Comp Sci 
  • Thread starter Thread starter pumas
  • Start date Start date
  • Tags Tags
    Error
Click For Summary

Discussion Overview

The discussion revolves around fixing an ArrayOutOfBound error encountered in a Java method designed to add integers to a sorted array and perform a binary search. The focus is on identifying the cause of the error and suggesting potential fixes, with an emphasis on algorithmic correctness and boundary conditions.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their method for adding integers to a sorted array and mentions encountering an ArrayOutOfBound error.
  • Another participant notes that the algorithm shifts elements to make space for the new value but raises a concern about the condition when the new value is less than all existing values, which could lead to accessing an invalid index.
  • A suggestion is made to add a condition to handle the case when the index is 0 to prevent accessing elementData[-1].
  • A different participant points out that the for loop starts at size, which exceeds the valid index range of the array, indicating a potential source of the error.

Areas of Agreement / Disagreement

Participants express differing views on the exact cause of the ArrayOutOfBound error, with some focusing on the loop's starting index and others on the handling of boundary conditions when inserting a new value. No consensus is reached on a definitive solution.

Contextual Notes

The discussion highlights limitations related to the assumptions about the array's size and the handling of edge cases during insertion. There are unresolved mathematical steps regarding the correct implementation of the binary search and the insertion logic.

Who May Find This Useful

Readers interested in Java programming, particularly those dealing with array manipulation, sorting algorithms, and error handling in coding practices.

pumas
Messages
15
Reaction score
0
I'm supposed to create a method that adds intergers to an array and keep it in sorted order. I'm supposed to search for the number in the array using binarySearch. I get an error that says arrayOutOfBound I don't know how to fix this. I have written the following code:
elementData is the array,

Code:
 public void add(int value) {

      int index = binarySearch(elementData,value,0, size - 1);
     
      }
      for (int i = size; i > index; i--)
          {
           elementData[i] = elementData[i - 1];
           elementData[index] = value; 
           size++;
          }
          
         }

I appreciate any help :confused:
 
Physics news on Phys.org
I'm a little rusty in my Java,
so your algorithm there seems to move every value up in order to make a spot to place the new value in the right sorted order right?

The only thing I can see is what happens when the new value is lower than all other values? so it goes all the way to i=0, and elementData[i-1] will give try to get to elementData[-1] which would be out of the boundary of the array.

So maybe adding and if(i=0) will fix that? so you just replace the 0'th element, since it was already moved up to [1].
 
Thanks for your help
 
Well, if the array's length is called size, then the elements of that array range from 0 to size - 1. However, your for loop starts with i=size, meaning you're past the end of the array.

- Warren
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
20
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K