Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何显示数组的元素而不是值_Java - Fatal编程技术网

Java 如何显示数组的元素而不是值

Java 如何显示数组的元素而不是值,java,Java,嗨,我正在尝试创建一段代码,生成一个随机长度为15的数组,然后给每个元素一个0-100之间的随机数,我希望能够测试点1(设置为0)和数组中存储的距离之间的距离,例如元素2的值为60,因此与点1的距离为60,我不想显示与点1的距离,而是想显示哪些点从最小到最大距离点1最近,到目前为止,我先将数组排序为最小的数字,但我想知道如何显示哪个元素存储了什么距离,而不是相反,例如,离点1最近的点(0)是第4点(89,距离第1点最近的第二个点是第3点(25),以下是我目前掌握的代码: static doubl

嗨,我正在尝试创建一段代码,生成一个随机长度为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]);
静态双距离(双x1,双x2)
{  
返回数学sqrt((x2-x1)*(x2-x1));
//Math.sqrt是两个坐标的平方根
}  
公共静态void main(字符串[]args)
{     
Random randomGenerator=新的Random();
双x2,x1;//点
x1=0;
双距离;//计算距离的数学
int randomInt2=randomGenerator.nextInt(15)+1;//表示数组的长度
double[]距离=新的double[randomInt2];//数组
双店;
System.out.println(“为点1:0生成的距离”);
System.out.println(“创建的点数为:“+randomInt2”);
int range=0;//点数
而(范围

如果您能提供任何帮助,我们将不胜感激。

这是一个经典但有趣的问题。您可以在第二个数组中保持点的顺序,但无法根据距离对它们进行排序

您使用的是真正的OOP还是类似C的算法代码

下面是我要做的:

  • 创建包含点的秩(例如,点4的秩为4)及其坐标(x?)的类点
  • 实现与点比较的接口,并根据距离进行比较(您知道该接口吗?)
  • 将点保留在点列表(例如ArrayList of Point)中,而不是数组中
  • 使用集合对列表进行排序。排序(列表)(如果内存可用)
  • 重新定义Point.toString,以便轻松打印列表
  • 排序列表将按距离对点进行排序,每个点都知道其排名


    如果我不清楚,请毫不犹豫地提问。

    您应该创建一个类点,使用距离和compareTo方法。compareTo方法应该测试哪个距离更大。 您应该使用点数组,而不是双数组,并且必须使用可比较的接口对其进行排序

    // 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);
        ....
    }
    
    //这是一个存根
    类Point()实现可比较的
    {
    公共双倍距离(p点){
    ....
    }
    公共国际比较(p点)
    {
    返回新的Double(this.distance()).compareTo(p.distance());
    }
    }
    公共静态void main(字符串[]args){
    ....
    点[]点=新点[15];
    ....
    数组。排序(点);
    ....
    }
    
    Java的
    SortedMap
    提供了一种按键排序的方法,同时保持与关联值的关系

    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());
    
    Distances (sorted): [0.5, 1.0, 2.0, 3.0]
    Indexes (sorted by distance): [3, 0, 2, 1]