0

Hi im trying to create a piece of code that generates an array of random length 15 and then gives each element a random number between 0-100, i want to be able to test the distance between point 1(which is set at 0) and the distances stored in the array e.g element 2 has value 60 so distance from point 1 is 60, instead of displaying the distance from point 1 i want to display which points are closest to point 1 from smallest to largest, so far i have sorted the array to be smallest numbers first however was wondering how i would go about showing which element stores what distance and not the other way around e.g the nearest point to point 1(0) is point 4(89, the 2nd nearest point to point 1 is point 3(25), here is the code i have so far:

static double distance(double x1, double x2)  
            {  
            return Math.sqrt((x2-x1)*(x2-x1) );
            //Math.sqrt is the square root of the 2 co-ordinates
            }  
public static void main(String[] args)  
{     

    Random randomGenerator = new Random();
    double x2, x1; //The Points
     x1 = 0;
    double distance;//The Math to work out distance  
    int randomInt2 = randomGenerator.nextInt(15)+1;//For Length of array  
    double [] distances = new double [randomInt2];//The Array
    double store;


    System.out.println("Distance Generated for point 1 : 0" );
    System.out.println("Amount of points created is: "+randomInt2 );

    int range = 0;//Amount of Points
    while (range < randomInt2) {  
    int randomInt3 = randomGenerator.nextInt(1000);//Distance for all points besides first     
    x2 = randomInt3;      
    distance = distance(x1,x2);      
    store = distance;//stores the distance to be put into array
    distances[range] = store;
    range++;//increments amount of points each time      
    }

    for (double val : distances) {
    System.out.print("["+val+"],");
    }

    Arrays.sort(distances);// sorts array from highest to lowest 
    System.out.println("\nThe Nearest to point 1 is: " + distances[0]);

Any help would be greatly appreciated

3 Answers 3

1

Java's SortedMap offers a way to sort by key while keeping relation to associated values.

double[] distances = new double[]{ 1, 3, 2, 0.5 };
SortedMap<Double, Integer> arrayIndexByDistance = new TreeMap<>();
for (int i = 0; i < distances.length; i++) {
    arrayIndexByDistance.put(distances[i], i);
}
System.out.println("Distances (sorted): " + arrayIndexByDistance.keySet());
System.out.println("Indexes (sorted by distance): " + arrayIndexByDistance.values());

Output:

Distances (sorted): [0.5, 1.0, 2.0, 3.0]
Indexes (sorted by distance): [3, 0, 2, 1]
Sign up to request clarification or add additional context in comments.

Comments

0

This is a classical yet interesting problem. You could keep the points order in a second array, but you wouldn't be able to sort them with the distances.

Do you work with real OOP or is this some C-like algorithm code only ?

Here's what I would do :

  1. Create class Point which contains the point's rank (e.g. point 4 has a rank of 4), and its coordinates (x?).
  2. Implement interface Comparable with Point, and base compareTo on the distance (do you know about this interface ?).
  3. Keep points in a List of Point (for instance ArrayList of Point) instead of an array.
  4. Sort the list with Collections.sort(list) (if memory serves).
  5. Redefine Point.toString so the list can easily be printed.

The sorted list will have the points sorted by distance, and each point knows its rank.

Do not hesitate to ask any questions if I'm unclear.

Comments

0

You should create a class Point, with distance and compareTo method. The compareTo method should test which distance is greather. Than you should have a Point array, rather than a double array and you must sort it using the Comparable interface.

// THIS IS A STUB
class Point() implements Comparable<Point>
{
    public double distance(Point p) {
        ....
    }

    public int compareTo(Point p)
    {
         return new Double(this.distance()).compareTo(p.distance());
    }

}

public static void main(String[] args) {
    ....
    Point[] points = new Point[15];
    ....
    Arrays.sort(points);
    ....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.