-1

Hi im trying to show the index of the array not the element this is part of my code so far:

int randomInt2 = randomGenerator.nextInt(15)+1;
double [] distances = new double [randomInt2];

Arrays.sort(distances);// sorts array from highest to lowest        
for (double val : distances) {
System.out.println("["+val+"],");
}

System.out.println("The Nearest to point 1 is point: ");

Each index holds an value between 1-1000 but i do not want to show the value i want to show the index so that I can show what indexes are closest to point 1 (which is 0) Sorry if im not being clear and if you need me to explain more then I am happy to

3
  • 2
    Use for loop to have control on the index. Commented Nov 20, 2013 at 10:45
  • Use for loop with iteration to show the index... Commented Nov 20, 2013 at 10:47
  • possible duplicate of How to show element of array instead of value Commented Nov 20, 2013 at 10:58

5 Answers 5

3

Thanks to dasblinkenlight's comment I finally understood what you need.

The easiest way to do what you need to do would be to create an object like

class Value implements Comparable<Value> {

  int index;
  dobule value;

  public int compareTo(Value val) {
    return Double.compare(this.value, val.value);
  }
}

And use it to store the values. Note the Comparable implementation - allows you to use Collections.sort() and Arrays.sort().

You could also not use sorting. Sorting an array is a much more complex operation than finding the minimum value. Just iterate over the array once and find the smallest value and return its index.

double minVal = Double.MAX_VALUE;
int minIndex = -1;
for (int i=0, max=distances.length; i<max;i++) {
    if (values[i] < minVal) {
        minVal = values[i];
        minIndex = i;
    }
}
System.out.println("The Nearest to point 1 is point: "+minIndex+" with value "+minVal);

As for indexes: you can't use standard foreach loop if you want to access the index of a given element. One of the reasons is that some collections you may iterate over do not support element ordering (like a Set).

You have to use standard for loop or track the index yourself.

for (int i=0, max=distances.length; i<max;i++) {
    System.out.println("["+i+"] "+distances[i]);
}

or

int i = 0;
for (double val : distances) {
    System.out.println("["+i+"] "+val);
    i++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks for the help i apprectiate it, sorry i havent really used compare before so not very useful with it however everytime i try to use the code it gets the error "compareTo(Value) in Value cannot implement compareTo(T) in Comparable attempting to assign weaker access priviliges;was public where T is a type-variable; T extends object declared in interface comparable" Any thoughts?
@user3004630 corrected the code. public modifier lacking for method declaration. I guess.
ah thank you very much was able to get it working :),really appreciate your help was wondering if there is a way of displaying all the values for example nearest to point 1 is point 3(34), then point 2(100)etc..?
@user3004630 you can and should come to a solution on your own.
1

try like this

for (int i=0;i< distances.length;i++) {
      System.out.println("distances["+i+"]");
}

1 Comment

This is quite useless, because it shows distances[0], distances[1], ... OP wants the original index, before the sort (see his other question to understand what he's doing). Unfortunately, once he sorts, that index is gone; he needs to change his approach.
1

I assume you want to find out the index of the item with lowest distance in the original array. When you sort the array, this information is lost, as it will simply indexed from 0..length-1. Therefore, your answer will always be 0.

You can do 2 things:
1) Find the minimum value:

double[] distances = new double [randomInt2];
// Need copy to for sorting
double[] c = Arrays.copyOf(distances, distances.length);
// Find minimum value
Arrays.sort(c);
double min = c[0];

// Search that value in the original array:
for (int i = 0; i < distances.length; ++i) {
  if (distances[i] == min) {
    System.out.println("Minimum distance at: " + i);
    break;
  }
}

2) Store the index information with the distance information:
For this you need to:

  1. write your own class, like public class Distance, with a distance and index member.
  2. Implement a Comperator<Distance>, so that instances are compared by distance
  3. Make a function like Distance[] convertToDistance(double[] array) that creates a Distance array from your pure double values (if needed)
  4. Sort the array using Arrays.sort(T[], Comparator<? extends T>) method
  5. Get the result from sortedDistances[0].index

Comments

0

You can just used an old fashioned for loop instead of an enhanced for loop:

Arrays.sort(distances);// sorts array from highest to lowest        
for (int i = 0; i < distances.lengh; ++i) {
    System.out.println("index: " + i + ":[" + val + "],");
}

Comments

0

you might every distance let be an object of

class Distance {
  public Distance(double dist) {
    this.dist = dist;
    this.id = idcount++;
  }

  static int idcount = 0;
  public int id;
  public double dist;
}

and than call the id in every

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.