How to get datas from List<Object> (Java)?

前端 未结 5 1944
我在风中等你
我在风中等你 2020-12-31 19:12

I`m new in Java and have a problem with showing data from a list of objects. I have a simple method, which should collect data across multiple tables and return it to my con

相关标签:
5条回答
  • 2020-12-31 19:22

    Do like this

    List<Object[]> list = HQL.list(); // get your lsit here but in Object array
    

    your query is : "SELECT houses.id, addresses.country, addresses.region,..."

    for(Object[] obj : list){
    String houseId = String.valueOf(obj[0]); // houseId is at first place in your query
    String country = String.valueof(obj[1]); // country is at second and so on....
    .......
    }
    

    this way you can get the mixed objects with ease, but you should know in advance at which place what value you are getting or you can just check by printing the values to know. sorry for the bad english I hope this help

    0 讨论(0)
  • 2020-12-31 19:34

    For starters you aren't iterating over the result list properly, you are not using the index i at all. Try something like this:

    List<Object> list = getHouseInfo();
    for (int i=0; i<list.size; i++){
       System.out.println("Element "+i+list.get(i));
    }
    

    It looks like the query reutrns a List of Arrays of Objects, because Arrays are not proper objects that override toString you need to do a cast first and then use Arrays.toString().

     List<Object> list = getHouseInfo();
    for (int i=0; i<list.size; i++){
       Object[] row = (Object[]) list.get(i);
       System.out.println("Element "+i+Arrays.toString(row));
    }
    
    0 讨论(0)
  • 2020-12-31 19:40

    Thanks All for your responses. Good solution was to use 'brain`s' method:

    List<Object> list = getHouseInfo();
    for (int i=0; i<list.size; i++){
    Object[] row = (Object[]) list.get(i);
    System.out.println("Element "+i+Arrays.toString(row));
    }
    

    Problem solved. Thanks again.

    0 讨论(0)
  • 2020-12-31 19:41
    System.out.println("Element "+i+list.get(0));}
    

    Should be

    System.out.println("Element "+i+list.get(i));}
    

    To use the JSF tags, you give the dataList value attribute a reference to your list of elements, and the var attribute is a local name for each element of that list in turn. Inside the dataList, you use properties of the object (getters) to output the information about that individual object:

    <t:dataList id="myDataList" value="#{houseControlList}" var="element" rows="3" >
    ...
    <t:outputText id="houseId" value="#{element.houseId}"/>
    ...
    </t:dataList>
    
    0 讨论(0)
  • 2020-12-31 19:43

    You should try something like this

    List xx= (List) list.get(0)
    String id = (String) xx.get(0)
    

    or if you have a House value object the result of the query is of the same type, then

    House myhouse = (House) list.get(0);
    
    0 讨论(0)
提交回复
热议问题