你能否告诉我是否可以创建用java中的对象创建的多维数组?
换句话说:
我有这门课:
public class ClosestObject {
public Double distance;
public String classes;
public ClosestObject() {
this.distance=new Double(0);
this.classes=new String("");
// TODO Auto-generated constructor stub
}
}
我想以这种方式在另一个类中创建多维数组:
ClosestObject[][] closestobj=new ClosestObject[query.length][learning_set.length];
for(int i=0; i<query.length;i++)
{
for(int j=0;j<learning_set.length;j++)
{
closestobj[i][j].distance=Math.abs(query[i]-learning_set[j][0]);
closestobj[i][j].classes=classes[j][0];
}
}
for(int i=0;i<closestobj.length;i++)
{
for(int j=0;j<closestobj[i].length;j++)
{
System.out.println(closestobj[i][j].distance + " " + closestobj[i][j].classes);
}
}
不幸的是我收到了这个错误:
Exception in thread "main" java.lang.NullPointerException
at KNN.Find_closest_distance(KNN.java:60)
at KNN.main(KNN.java:37)
有人能告诉我哪里弄错了。提前谢谢你:)