简体   繁体   中英

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 

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);
    }

You're calculating the distance between 2 points but not saving that result anywhere:

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));
    //what happens with distance?
}
//you get only the last distance between the last points.
result.add(distance);

One more thing, your distance variable is an integer that will try to hold double values, is that right?

You need to refactor things to simplify I think. First and foremost, create a simple manhattanDist method that accepts two Point objects and that returns an int:

public int manhattanDist(Point p1, Point p2) {
  // calc the manhattan distance and return it
}

Then you can easily use this method when comparing your ArrayList<Point> without mixing up things in your for loops as you're doing above.

Also, please learn and use Java naming conventions including:

  • method names should begin with a lower case letter
  • class names should begin with an upper case letter.
  • identifier names should be logical, should make sense, and should make your code self-commenting.

This is only important if you want others (such as your instructors, or us ) to more easily and quickly understand your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM