如何从 List<Object> (Java) 获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get datas from List<Object> (Java)?
提问by charles5300
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 controller:
我是 Java 新手,在显示对象列表中的数据时遇到问题。我有一个简单的方法,它应该跨多个表收集数据并将其返回给我的控制器:
public List<Object> getHouseInfo(){
Query q = em.createNativeQuery("SELECT houses.id, addresses.country, addresses.region, house_details.rooms, house_details.square FROM houses, addresses, house_details");
List<Object> myList = q.getResultList();
return myList;}
Now I want to get this data in controller, but I don`t know how to get single results from the list. I tried to do something like this:
现在我想在控制器中获取这些数据,但我不知道如何从列表中获取单个结果。我试图做这样的事情:
List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(0));}
but I`m getting only references to this objects (for example [Ljava.lang.Object;@167a47b). I also tried to use Iterator, but the result is the same. I tried to use a code like this:
但我只得到对这个对象的引用(例如 [Ljava.lang.Object;@167a47b)。我也尝试过使用 Iterator,但结果是一样的。我尝试使用这样的代码:
List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(0)[0]);}
but it doesn`t help me too - this ends with a compile error.
但这对我也没有帮助 - 这以编译错误结束。
Can someone tell me how to get an 'id'(integer value) from this list? I`m using MyFaces in my 'View' where I have a code like this (houseControll is the name of my JSF Managed Bean - the controller):
有人可以告诉我如何从此列表中获取“id”(整数值)吗?我在我的“视图”中使用 MyFaces,其中有这样的代码(houseControll 是我的 JSF 托管 Bean 的名称 - 控制器):
<t:dataList id="myDataList" value="#{houseControll.fullOffer}" var="element" rows="3" >
...
<t:outputText id="houseId" value="#{element[0]}"/>
...
</t:dataList>
this code shows an 'id' value properly - I have 1,2,3,... values. How can I get the same result in my controller? How to print the data in controller?
这段代码正确显示了一个“id”值 - 我有 1,2,3,... 值。如何在我的控制器中获得相同的结果?如何打印控制器中的数据?
回答by GaryF
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:
要使用 JSF 标记,您需要为 dataList 值属性提供对元素列表的引用,而 var 属性依次是该列表中每个元素的本地名称。在 dataList 中,您使用对象的属性(getter)来输出有关该单个对象的信息:
<t:dataList id="myDataList" value="#{houseControlList}" var="element" rows="3" >
...
<t:outputText id="houseId" value="#{element.houseId}"/>
...
</t:dataList>
回答by HamoriZ
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 值对象,查询的结果是相同的类型,然后
House myhouse = (House) list.get(0);
回答by brain
For starters you aren't iterating over the result list properly, you are not using the index i at all. Try something like this:
对于初学者,您没有正确迭代结果列表,您根本没有使用索引 i 。尝试这样的事情:
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().
看起来查询返回了一个对象数组列表,因为数组不是覆盖 toString 的正确对象,您需要先进行强制转换,然后使用 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));
}
回答by charles5300
Thanks All for your responses. Good solution was to use 'brain`s' method:
感谢大家的回复。好的解决方案是使用 'brain`s' 方法:
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.
问题解决了。再次感谢。
回答by Sumit
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,..."
您的查询是:“选择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
通过这种方式,您可以轻松获得混合对象,但您应该提前知道在哪个位置获得了什么值,或者您可以通过打印值来了解。抱歉英语不好我希望这会有所帮助