views:

41

answers:

1

Hello

I have a java backing bean which has a method (call it getDataList()) which returns an ArrayList<MyType>.

In MyType.java I have a load of setters and getters all of which either assign Strings or String[]s. An example instantiation assigns 4 objects to the ArrayList, containing (among other) the following data:

fid = 1    ftid = 1
fid = 2    ftid = 1
fid = 3    ftid = 1
fid = 3    ftid = 2

In my jsf code I'm referencing the structure into a rich:dataTable. Trouble is the table comes out like this:

fid        ftid
1          1
2          1
3          2      # should be 3, 1
3          2

As you can see the last-but-one iteration is incorrect. i've tried this with a h:dataTable and the same happens so I know it's probably not richfaces. but my java console through eclipse(ganymede/tomcat6) shows the correct assignment:

|STDOUT| 2010-02-08 15:23:58,953 | INFO  | [http-8989-5]: adding ftqId 1 feedId 1
|STDOUT| 2010-02-08 15:23:58,984 | INFO  | [http-8989-5]: adding ftqId 1 feedId 2
|STDOUT| 2010-02-08 15:23:59,000 | INFO  | [http-8989-5]: adding ftqId 1 feedId 3
|STDOUT| 2010-02-08 15:23:59,000 | INFO  | [http-8989-5]: adding ftqId 2 feedId 3

can anyone tell me how to extract the data from my ArrayList object correctly? Should I use a hashmap?

As per request here are the smallest snippets of relevant code (thanks Colin):

This is the bean code:

public ArrayList<MyType> getFDataList() {
    XMLHandler handler = new XMLHandler();
    dataList = new ArrayList<MyType>();
    try {Document doc = handler.XMLDoc("Config.xml");} // catches removed
    String[] fIds = handler.XMLList("//snip/fid/text()");
    for (String fId : fIds) {           
        MyType tld = new MyType();
        String[] ftIds = handler.XMLList("//snip/ftid/text()");
        for (String ftId : ftIds) {
            tld.setFtId(ftId);
            tld.setFId(fId);        
            logger.info("ftId:"+ftId+",fId:"+fId);
            dataList.add(tld);  // each ft
        }
    }
    return dataList;
}

This is the supporting class code:

public class MyType {
    private String fId;
    private String ftId;

    public String getFId() {  
        return fId;  
    }  

    public void setFId(String f) {  
        fId = f;  
    } 

    public String getFtId() {  
        return ftId;  
    }  

    public void setFtId(String fi) {  
        ftId = fi;  
    } 
}

The only thing I can think of is that I'm nesting the work done which adds the object to the ArrayList, but I don't think that should matter. Thanks in advance.

+3  A: 

You should be creating a new MyType object each time in the inner loop. What is happening is that you are only creating one instance of the object for each fId, but for each ftId you are overwriting the value and adding it to the list. You end up with multiple references to the same object.

Move the instantiation inside the inner loop like this.

   for (String fId : fIds) {         
        String[] ftIds = handler.XMLList("//snip/ftid/text()");
        for (String ftId : ftIds) {  
            MyType tld = new MyType();
            tld.setFtId(ftId);
            tld.setFId(fId);        
            logger.info("ftId:"+ftId+",fId:"+fId);
            dataList.add(tld);  // each ft
        }
    }
Colin Gislason
+1 well spotted.
BalusC
Thanks very much indeed.
Mark Lewis