gpt4 book ai didi

Java 基于位置数组对坐标进行分组

转载 作者:行者123 更新时间:2023-12-01 10:22:01 25 4
gpt4 key购买 nike

我有一个 x 数组,其中包含一些 double 组,如下所示:

 double x[] = {2, 4, 1, 1};

我还有 3 个数组,用于定义每个组中 x 值的位置:

 int[] cluster1 = {1}; //which means that first value in x array belongs to cluster 1
int[] cluster2 = {2}; //second value in x array belongs to cluster 2
int[] cluster3 = {3, 4}; //third value & forth value in x array belongs to cluster 3.

如何获得如下输出?

Cluster 1: (2)
Cluster 2: (4)
Cluster 3: (1), (1)

还值得注意的是,有时相同的值可能会在不同的簇中出现两次。感谢您的帮助!

最佳答案

这是一种实现方法:

public static void main (String[] args)
{
/* Values Array */
double x[] = {2, 4, 1, 1};
double y[] = {1, 3, 1, 9};

/* Clusters Array */
Double[] cluster1 = {1.0};
Double[] cluster2 = {2.0};
Double[] cluster3 = {3.0, 4.0};

/* Print Cluster 1 Values */
System.out.print("Cluster 1:");
for(int i = 0; i < cluster1.length; i++) {
System.out.print(" (" + x[cluster1[i].intValue() - 1] + "," + y[cluster1[i].intValue() - 1] + ")");
}

/* Print Cluster 2 Values */
System.out.print("\nCluster 2:");
for(int i = 0; i < cluster2.length; i++) {
System.out.print(" (" + x[cluster2[i].intValue() - 1] + "," + y[cluster2[i].intValue() - 1] + ")");
}

/* Print Cluster 3 Values */
System.out.print("\nCluster 3:");
for(int i = 0; i < cluster3.length; i++) {
System.out.print(" (" + x[cluster3[i].intValue() - 1] + "," + y[cluster3[i].intValue() - 1] + ")");
}
}

输出:

Cluster 1: (2.0,1.0)
Cluster 2: (4.0,3.0)
Cluster 3: (1.0,1.0) (1.0,9.0)

关于Java 基于位置数组对坐标进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35533713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com