I have a method that takes in a rectangle shape as a parameter, uses it as a selection box, and finds shapes in an array called shapeObjects that are within the bounds of the selection box. The indexes of those shapes are then added into a new array which is then returned.

// Searches for shapes that matches the selection's bounding area
  // and returns their indexes in the shapeObjects array
  public int[] findSelectedShapes( Shape selection ) {
    
    // Integer array for return
    int shapeIndexes[] = new int[100];
    int numMatching = 0;
    
    // Refers current shape object being checked
    int obj = 0;
    // Shape coordinates
    int x1;
    int x2;
    int y1;
    int y2;
    
    // Iterate checking as long as the object index is less than that of the shapeObjects array length
    while( obj < numOfShapes ) {
      
      // Actual object
      Shape shape = shapeObjects[obj];
      // Coordinates of the shape
      x1 = shape.getX1();
      x2 = shape.getX2();
      y1 = shape.getY1();
      y2 = shape.getY2();
      
      // Range between x2 and x1, and y2 and y1 of the selection
      for( int i = selection.getX2(); i >= selection.getX1(); i-- ) {
        for( int j = selection.getY2(); j >= selection.getY2(); j-- ) {
          
          // Check the shape's coordinates
          // If any of the points match
          if( ( x1 == i) || ( x2 == i) || (y1 == j) || (y2 == j) ) {
            
            // Add shape index to the array
            shapeIndexes[ numMatching ] = obj;
            numMatching++;
            
          }
          
        }
      }
      
      obj++; // Increase the counter
      
    }
    
    // Update screen
    repaint();
    
    // Array with size of matching
    int ret[] = new int[ numMatching ];
    ret = shapeIndexes;
    // Return the selected shapes' indexes
    return ret;
    
  }

When I print out the indexes it has found however, I get a million zeroes around a few numbers. Few things I want to do:
1) Get rid of the extra zeroes. I thought that creating a new array with a new size would work, but apparently not.
2) Make it find the right indexes. I currently can't find the problem in this algorithm that is making it find random shapes.

Thanks for any help.

Loop the array and print only those that are not zero. Also I would suggest to initialize the array: shapeIndexes to have all -1 as initial elements. With the way you initialize it, it begins with zeros. So if it happens that the selected shape is the first element of shapeObjects array, the shapeIndexes array will have the index 0, but you wouldn't know the difference between that zero and the one that the array initially had.
If you make that change then at your loop make sure to print only those indexes that are not -1

As for your algorithm I would drop the 2 for loops and use if statements that compare their coordinates to see if the shape's coordinates are inside/between the selected shapes:

if (selection.getX1() <= shape.getX1() && selection.getX2() >= shape.getX2())

The above will tell you if the X coordinates of the shape are inside the selection, assuming that x1<x2 always.
Do the same for the y coordinates

This article has been dead for over six months. Start a new discussion instead.