鉴于以下代码,由于某种原因它不会创建 MyVector 的实例。可能是什么问题?问题出现在 Main 行:
MyVector vec = new MyVector();
但是,当我使用另一个构造函数创建 MyVector 的实例时:
MyVector vec2 = new MyVector(arr);
编译并分配实例。
类点:
public class Dot {
private double dotValue;
public Dot(double dotValue)
{
this.dotValue = dotValue;
}
public double getDotValue()
{
return this.dotValue;
}
public void setDotValue(double newDotValue)
{
this.dotValue = newDotValue;
}
public String toString()
{
return "The Dot's value is :" + this.dotValue;
}
}
MyVector 类
public class MyVector {
private Dot[] arrayDots;
MyVector()
{
int k = 2;
this.arrayDots = new Dot[k];
}
public MyVector(int k)
{
this.arrayDots = new Dot[k];
int i = 0;
while (i < k)
arrayDots[i].setDotValue(0);
}
public MyVector(double array[])
{
this.arrayDots = new Dot[array.length];
int i = 0;
while (i < array.length)
{
this.arrayDots[i] = new Dot(array[i]);
i++;
}
}
}
和主要
public class Main {
public static void main(String[] args) {
int k = 10;
double [] arr = {0,1,2,3,4,5};
System.out.println("Enter you K");
MyVector vec = new MyVector(); // that line compile ,but when debugging it crashes , why ?
MyVector vec2 = new MyVector(arr);
}
}
问候 罗恩
最佳答案
我将您的代码复制到 Eclipse IDE 中,并收到“org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: 在检索数组的组件类型时发生类型尚未加载”。当我单击 arrayDots 变量时出现异常。
您的代码正常并且可以工作。调试器出现问题,因为未加载 Dot 类。 另请参阅:http://www.coderanch.com/t/433238/Testing/ClassNotLoadedException-Eclipse-debugger
您可以按如下方式更改 Main(我知道这不是很漂亮)
public static void main(String[] args) {
int k = 10;
double [] arr = {0,1,2,3,4,5};
System.out.println("Enter you K");
new Dot(); // the classloader loads the Dot class
MyVector vec = new MyVector(); // that line compile ,but when debugging it crashes , why ?
MyVector vec2 = new MyVector(arr);
}
关于java - 为什么我无法创建 MyVector 的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8334012/