2015-07-09 104 views
0

我添加到我的ArrayList “activList” 元素使用:打印内存地址,而不是字符串值

activList.add(new Fiducial("R600")); 

我使用打印:

System.out.println(activList); 

下面是代码:

// Grid variables 
int cols = 10, rows = 10; 
int rectangleWidth = 100; 
int rectangleHeight = 60; 

// these are some helper variables which are used 
// to create scalable graphical feedback 
int k, l, iD; 
float cursor_size = 15; 
float object_size = 60; 
float table_size = 760; 
float scale_factor = 1; 
float x, y; 


ArrayList<Fiducial> activList = new ArrayList<Fiducial>(); 

public class Fiducial { 

    public String type; 

    public Fiducial(String type) { 

    this.type = type; 
    } 

} 

void draw() { 
    // Begin loop for columns 
    for (k = 0; k < cols; k++) { 
    // Begin loop for rows 
    for (l = 0; l < rows; l++) { 
     fill(255); 
     stroke(0); 
     rect(k*rectangleWidth, l*rectangleHeight, rectangleWidth, rectangleHeight); 
    } 
    } 




    // This part detects the fiducial markers 
    float obj_size = object_size*scale_factor; 
    float cur_size = cursor_size*scale_factor; 

    ArrayList<TuioObject> tuioObjectList = tuioClient.getTuioObjectList(); 
    for (int i=0; i<tuioObjectList.size(); i++) { 
    TuioObject tobj= tuioObjectList.get(i); 
    stroke(0); 
    fill(0, 0, 0); 
    pushMatrix(); 
    translate(tobj.getScreenX(width), tobj.getScreenY(height)); 
    rotate(tobj.getAngle()); 
    rect(-80, -40, 80, 40); 
    popMatrix(); 
    fill(255); 
    x = round(10*tobj.getX()); 
    y = round(10*tobj.getY()); 
    iD = tobj.getSymbolID(); 
    activList.add(new Fiducial("R600")); 
    fiducialVisibles(); 

    } 

} 


void fiducialVisibles() { 
System.out.println(activList); 
} 

但是,它返回类似[FiducialDetection $ Fiducial @ 47baec4c]这似乎是地址。我如何检索字符串的实际值?

回答

1
public class Fiducial { 

    @Override 
    public String toString() { 
     String yourResult = this.type; // + ... 
     return yourResult; 
    } 

} 

您必须在班级中覆盖toString()方法。

+0

感谢它运作良好!与int和float相同? –

+0

@MatthiasGrahamSlick,'String yourResult = Integer.toString(this.intVariable)+ Float.toString(this.floatVariable)+ ....;'并将'yourResult'返回给'toString()'方法。 – Andrew

+0

@MatthiasGrahamSlick,理想情况下''toString()'方法必须返回类中所有字段的字符串表示 – Andrew

2

在你的Fiducial类上实现toString()。

+0

尝试在这里给出一些例子。 – Nakul91

相关问题