类型FastVector<E>已被弃用。

7

我正在尝试从Java的多维数组中获取arrf扩展输出文件。我已经导入了weka库,但是出现了一个错误:FastVector<E>类型已过时。

我可以使用什么替代FastVector,并且如何重写下面的代码?

    import weka.core.FastVector; //Error: The type FastVector<E> is deprecated. 


    int [][] myArray = new int[45194][12541];

    for (int i = 0; i < myArray.length; i++) {
        for (int j = 0; j < myArray[0].length; j++) {
            System.out.print(myArray[i][j]+" "); 
        }
        System.out.println("");
    }

    int numAtts = myArray[0].length;
    FastVector atts = new FastVector(numAtts);
    for (int att = 0; att < numAtts; att++) {
        atts.addElement(new Attribute("Attribute" + att, att));
    }

    int numInstances = myArray.length;
    Instances dataset = new Instances("Dataset", atts, numInstances);
    for (int inst = 0; inst < numInstances; inst++) {
        dataset.add(new Instance(1.0, myArray[inst]));   //Error: Cannot instantiate the type Instance
    }

    BufferedWriter writer = new BufferedWriter(new FileWriter("test.arff"));
    writer.write(dataset.toString());
    writer.flush();
    writer.close();
1个回答

15
Weka现在大多数地方都使用了类型化的ArrayList。您可以使用 ArrayList<Attribute> 来实现此功能:
ArrayList<Attribute> atts = new ArrayList<Attribute>();
    for (int att = 0; att < numAtts; att++) {
        atts.add(new Attribute("Attribute" + att, att));
    }

2
是的,我明白,但是我如何在上面的代码中使用ArrayList? - EngineerEngin
我编辑了我的回复并添加了一段代码片段。只需要更改您的atts变量的类型就可以了,非常简单。 - Chris
但是,类型Instance的实例化问题仍然持续存在。你对这个错误有任何想法吗? - EngineerEngin
@EngineerEngin Instance 是一个接口。你不能直接创建一个接口的实例,但是你可以创建一个实现的实例。例如,SparseInstance - Obicere
那么,我该如何重写我的代码呢?我尝试使用DenseInstance,但是出现了错误: Instances dataset = new DenseInstance("Dataset", atts, numInstances); - EngineerEngin
尝试使用以下代码创建实例数据集:Instances dataset = new DenseInstance(numInstances); 然后逐个添加属性。 - Sudheera

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接