Привет, я пытаюсь создать кусок кода, который генерирует массив случайной длины 15, а затем дает каждому элементу случайное число от 0 до 100, я хочу проверить расстояние между точкой 1 (которое установлено в 0) и расстояния, хранящиеся в массиве, например, элемент 2 имеет значение 60, поэтому расстояние от точки 1 равно 60, вместо отображения расстояния от точки 1 я хочу показать, какие точки ближе всего к точке 1 от наименьшего к самому большому, до сих пор я отсортировал массив как наименьшее число, но мне было интересно узнать, как я буду показывать, какой элемент хранит, какое расстояние, а не наоборот, например, ближайшая точка к точке 1 (0) является точкой 4 (89, вторая ближайшая точка к точке 1 - точка 3 (25), вот код, который у меня до сих пор:Как показать элемент массива вместо значения
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]);
Любая помощь была бы принята с благодарностью