-1

I have an arraylist which stores coordinate points as objects, I also have created a formula to calculate the distance between these respective points. I need to store each point with their respective distances in a 2D arraylist. For example if I have points (1,2),(3,4),(5,6),(7,8) my 2D arraylist should store 3 distances for point (1,2), two distances for point (3,4) and one distance for point (7,8) if point (1,2) is my start point.

I have tried creating a nested for loop which keeps track of where my point is currently while simultaneously adding the distances in each index based on the sample point visited. However I get an Out of Bounds exception. I have verified that all points are stored correctly.

public static ArrayList<ArrayList<Double>> distance(ArrayList<sample_points> points) {

    ArrayList<ArrayList<Double>> distArray =newArrayList<ArrayList<Double>>();
    ArrayList<Double> distances = new ArrayList<Double>();

    double dist = 0;

    for(int i =0;i<points.size();i++){
        distArray.add(new ArrayList());
    }
     for (int i = 0; i<points.size(); i++) {
        //ArrayList<Double> distances = new ArrayList<>();  // convenience, you don't need the first loop only to populate the distArray lists
        for(int j=i+1; j<points.size(); j++){
            dist = Math.sqrt(
                    Math.pow(points.get(i).getX() - points.get(j).getX(), 2)
 + Math.pow(points.get(i).getY() - points.get(j).getY(), 2)
            );   // do your calculations here
            distances.add(dist);   // add the distance to the current distances list
        }
        distArray.add(distances); // add the current distances list to the result list
    }
    System.out.print("Your distances: "+ distarray)
    return distArray;
}

Expected results: If number of sample points are 4 i.e (p1,p2),(p3,p4),(p5,6),(p7,p8) where p(i) are arbitrary points:

Your distances: [[d1,d2,d3],[d4,d5],[d6]] where d(i) are arbitrary distances

Actual results: Your distances: [[d1,d2,d3,d4],[d1,d2,d3,d4],[d1,d2,d3,d4][d1,d2,d3,d4]]

D.Khumalo
  • 35
  • 7
  • 2
    maybe you should write `i < points.size()` instead of `i <= points.size()` – Chris623 Aug 16 '19 at 07:31
  • I have tried that it still gives me the same error – D.Khumalo Aug 16 '19 at 07:34
  • When I test your code there is no exception and the results seem to be correct too. The problem is probably in a differnt part of the code. Can you edit the question and add the stack trace of your `OutOfBoundsException`? – Tobias Aug 16 '19 at 07:42
  • I have edited the Actual results, the OutOfBoundsException does not occur anymore instead it prints the same distances in each index which is incorrect – D.Khumalo Aug 16 '19 at 08:58
  • Please see my revised answer. FYI: next time open a new question when your first problem does no longer occur or wait until the answer was revised :) . Otherwise people won't see your new question (since the first one was already marked as duplicate) – GameDroids Aug 16 '19 at 09:42

1 Answers1

0

Let's say the list points has n elements. Then your for-loop here for (int i = 0; i <= points.size(); i++) will end when i=n. But you can't access points at index n because it doesn't exist. Therefore points.get(i) will throw an IndexOutOfBoundsException once i==n (or i==points.size()).

Have a look at your indices one step at a time. Say your list points contains 3 elements:

// first iteration
i=0  
  j=1, j=2
// second iteration
i=1
  j=2
// third iteration
i=2
  // no j-loop because j=3 and 3 == points.size()
// fourth iteration
i=3 // that should not be possible because points has only 3 elements

Let me try to revise the loops a bit for you (EDIT: I forgot to mention that the rest of the method is different too. For example the initialization of the distArray)

public static void main(String[] args) {
    //this is just for testing. 
    ArrayList<sample_points> points = new ArrayList<>();
    points.add(new sample_points(1D, 1D));
    points.add(new sample_points(3D, 3D));
    points.add(new sample_points(5D, 5D));
    ArrayList<ArrayList<Double>> distance = distance(points);
}


public static ArrayList<ArrayList<Double>> distance(ArrayList<sample_points> points) {
    ArrayList<ArrayList<Double>> distArray = new ArrayList<>();  //here the result list

    for (int i = 0; i < points.size() - 1; i++) {   // you actually don't need the last iteration because the j-loop won't run anyway
        ArrayList<Double> distances = new ArrayList<>();  // convenience, you don't need the first loop only to populate the distArray lists
        for (int j = i + 1; j < points.size(); j++) {
            Double dist = Math.sqrt(
                    Math.pow(points.get(i).getX() - points.get(j).getX(), 2)
                            + Math.pow(points.get(i).getY() - points.get(j).getY(), 2)
            );   // do your calculations here
            distances.add(dist);   // add the distance to the current distances list
        }
        distArray.add(distances); // add the current distances list to the result list
    }
    System.out.print("Your distances: " + distArray);
    return distArray;
}

When I run this, I get:

Your distances: [[2.8284271247461903, 5.656854249492381], [2.8284271247461903]]

I did't check the results but it seems correct

dist (1,1) to (3,3) is 2.82....
dist (1,1) to (5,5) is 5.65....
and
dist (3,3) to (5,5) is also 2.82....

GameDroids
  • 5,338
  • 6
  • 34
  • 54