java - 为什么我无法创建 MyVector 的实例?

标签 java constructor

鉴于以下代码,由于某种原因它不会创建 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/

相关文章:

java - Method.invoke可以带什么参数?

java - 多线程测试

java - 既然他们停止为 Eclipse 开发 derby 插件,是否可以在 Eclipse 中使用来自 apache 的 derby?

java - 第一次使用数组,尝试创建频率表

c++ - 如果我传递临时引用并将其存储为类成员会怎样?

C++ 避免构造对象

c++ - 结构作为构造函数参数(在 main 中声明)

java - 如何使用正则表达式解析键值

java - 将鼠标单击时的图像添加到 JPanel

java - hibernate 默认值