Search code examples
javadistanceproximity

Calculating Manhattan distance in Java


I am creating a simple program to calculate proximity distance measures of coordinates read from text file, I want to create method to calculate manhattan distance of given points for example:

(0,1,1,0,1), (1,0,0,0,1), (0,0,0,1,1)
would result in:
      Item1 Item2 Item3
Item1  0    3     3
Item2  3    0     2
Item3  3    2     0

Manhattan Method:

public static void Manhattan(ArrayList<Points> x) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        int distance = 0;
        for(int ii=0;ii<x.size();ii++) {
            for(int jj=0; jj<x.get(ii).coordinates.size();jj++) {
                 distance = Math.abs(x.get(ii).coordinates.get(jj)) + Math.abs(x.get(ii).coordinates.get(jj));
            }   
            result.add(distance);
        }
        for(int ii=0;ii<result.size();ii++) {
            for(int jj=0; jj<result.size();jj++) {
                System.out.print(result.get(ii));
            }
            System.out.print(" ");
        }

    }

Class Point:

import java.util.ArrayList;
public class Points {
    ArrayList<Integer> coordinates = new ArrayList<Integer>();
    public Points (ArrayList<Integer> coordinates) {
        this.coordinates = coordinates;
    }
    public ArrayList<Integer> getCoordinates() {
        return coordinates;
    }
    public void setCoordinates(ArrayList<Integer> coordinates) {
        this.coordinates = coordinates;
    }
}

the problem is that I get weird results when I run the method, anyone knows what's the problem?

result: 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 222222222222222 

Solution

  • A Integer can not represent a coordinate. You can create something like -

    public class Coordinate {
        private int x;
        private int y;
    
        //...getter/setter/constructor ...
    }
    

    which can represent a coordinate.

    And instead of (list of just integers)

    ArrayList<Integer> coordinates = new ArrayList<Integer>();
    

    use (list of coordinates)

    List<Coordinate> coordinates = new ArrayList<Coordinate>();
    

    Now, if you define a method as @Hovercraft suggested (for Coordinate) it will be really easy to calculate distance between all the points to all other points (including itself)

    for(int i=0; i<coordinates.size(); i++) {
        for(int i=0; i<coordinates.size(); i++) {
            System.out.println(manhattnDist(coordinates.get(i), coordinates.get(j)));
        }
    }
    

    One obvious problem in your code

        int distance = 0;
        for(int ii=0;ii<x.size();ii++) {
            for(int jj=0; jj<x.get(ii).coordinates.size();jj++) {
                 //you keep assigning new values 
                 distance = Math.abs(x.get(ii).coordinates.get(jj)) + Math.abs(x.get(ii).coordinates.get(jj));
            }   
            //and then you add
            result.add(distance);
        }