有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java经过一段时间的循环:为什么刚刚写入的数组是空的?

我们目前正在尝试用java实现kmeans算法。我们的问题是:

我们使用getData()方法用文件中的数据填充二维数组。在getData()方法的while循环中,我们有一个println(),在return命令之前还有另一个

第一个println()为我们提供了刚刚从文件中获得的正确值

第二个println()只为该数组中的每个字段提供0.0,除了arrayList[299][0]

为什么呢

class KMeans {

    // Number of clusters
    int numberOfClusters = 4;
    // Starting point for each cluster (these values should be better than completely random values for our given data set)
    static double[] a = new double[]{-1.5, 2.0};
    static double[] b = new double[]{-1.5, 7.0};
    static double[] c = new double[]{1.5, 7.0};
    static double[] d = new double[]{1.5, 2.0};
    static double[][] pointArray;

    // This calculates the distance between a given point from the data set and a centroid
    public static double calculateDistance(double[] point, double[] centroid) {
        // get difference for X coordinates
        double maxX = Math.max(point[0], centroid[0]);
        double minX = Math.min(point[0], centroid[0]);
        double differenceX = maxX - minX;
        double differenceXSquared = Math.pow(differenceX, 2);

        // get difference for Y coordinates
        double maxY = Math.max(point[1], centroid[1]);
        double minY = Math.min(point[1], centroid[1]);
        double differenceY = maxY - minY;
        double differenceYSquared = Math.pow(differenceY, 2);

        // The whole thing is nothing other than pythagoras
        double zSquared = differenceXSquared + differenceYSquared;
        double z = Math.sqrt(zSquared);
        return z;
    }

    // This calculates which of the given distances is the lowest
    public static double[] nearestCluster(double e, double f, double g, double h) {
        double x = Math.min(e, f);
        double y = Math.min(x, g);
        double z = Math.min(y, h);

        if (z == e) {
            return a;
        }
        if (z == f) {
            return b;
        }
        if (z == g) {
            return c;
        } else {
            return d;
        }
    }

    // Read the file 
    public static double[][] getData() {
        try (BufferedReader br = new BufferedReader(new FileReader("/home/john/Downloads/data.txt"))) {
            String line;
            int i = 1;
            int j = 0;
            while ((line = br.readLine()) != null) {
                // Create the array in which we store each value
                pointArray = new double[i][4];
                //Splits each line a the space and writes it to an array
                String[] split = line.split("\\s+");

                // Cast the strings to double and write them to our pointArray         
                pointArray[j][0] = Double.parseDouble(split[0]);
                pointArray[j][1] = Double.parseDouble(split[1]);
                System.out.println(pointArray[0][0]);
                i++;
                j++;
            }
        } catch (IOException e) {
        }
        System.out.println(pointArray[0][0]);        
        return pointArray;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        pointArray = getData();
        for (double[] x : pointArray) {
            double distanceA = calculateDistance(x, a);
            double distanceB = calculateDistance(x, b);
            double distanceC = calculateDistance(x, c);
            double distanceD = calculateDistance(x, d);

            // Assigns the closest cluster to each point (not too efficent because we call the function twice, but it works)
            x[2] = nearestCluster(distanceA, distanceB, distanceC, distanceD)[0];
            x[3] = nearestCluster(distanceA, distanceB, distanceC, distanceD)[1];
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    排队

    pointArray = new double[i][4];
    

    每次通过循环时重新初始化数组。实际上,除了你读到的最后一行,你把所有的值都扔掉了

    相反,使用ArrayList来保存每一行。在while循环之前设置如下:

    List<Double[]> pointList = new ArrayList<>();
    

    然后你可以在每一行添加如下内容:

    Double[] points = new Double[4];
    // ...
    points[0] = Double.parseDouble(split[0]);
    // etc.
    pointList.add(points);
    

    然后返回pointList或将其转换为数组以返回