2011-07-10 35 views
5

所以我想用WEKA的Java API來創建一個新的字符串屬性...創造秧雞的Java API

通過API的javadoc閱讀字符串屬性,看來,這樣做的方法是使用此構造函數:

Attribute 

public Attribute(java.lang.String attributeName, 
       FastVector attributeValues) 

    Constructor for nominal attributes and string attributes. If a null vector of attribute values is passed to the method, the attribute is assumed to be a string. 

    Parameters: 
     attributeName - the name for the attribute 
     attributeValues - a vector of strings denoting the attribute values. Null if the attribute is a string attribute. 

,但我堅持爲我要傳遞到attributeValues參數是什麼......

當我把在空,Java的抱怨保護對象
當我把空,它的語法錯誤
當我把在new FastVector(),就變成了標稱屬性是空的,而不是一個字符串屬性...
當我創建一個新的對象:

FastVector fv = new FastVector(); 
fv.addElement(null); 

再通入FV的說法,它返回空指針異常...

究竟應該將什麼東西放到attributeValues參數中,以便它變成一個字符串屬性?

回答

8

您必須將null轉換爲FastVector。否則,更多的方法將適用於方法簽名:

FastVector attributes = new FastVector(); 
    attributes.addElement(new Attribute("attr", (FastVector) null)); 

這裏是在飛行中創建實例的一個很好的資源:http://weka.wikispaces.com/Creating+an+ARFF+file

3

打造WEKA STRING屬性簡單的方法是這樣的:

new Attribute("Distribution_weight",(FastVector) null); 

主要問題是WEKA在新類型的Java編輯器中使用導入的weka.jar和拋出異常模式定義NULL值或NULL向量。