将 Object[] 数组转换为 Java 中的 List
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28973317/
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
Converting Object[] array to List in java
提问by Randy
on a primefaces 3.5 form I created with a datatable the rowKey value is not being returned as a List, therefore I am getting the following error:
在我用数据表创建的 primefaces 3.5 表单上,rowKey 值没有作为列表返回,因此我收到以下错误:
JBWEB000309: type JBWEB000066: Exception report
JBWEB000068: message For input string: "foreignPartyId"
JBWEB000069: description JBWEB000145: The server encountered an internal error that prevented it from fulfilling this request.
JBWEB000070: exception
javax.servlet.ServletException: For input string: "foreignPartyId"
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
JBWEB000071: root cause
java.lang.NumberFormatException: For input string: "foreignPartyId"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:492)
java.lang.Integer.parseInt(Integer.java:527)
javax.el.ListELResolver.toInteger(ListELResolver.java:407)
javax.el.ListELResolver.getValue(ListELResolver.java:199)
...
I know the reason is because I am trying to populate the datatable with values from an Object[] array, rather than a List. The Object[] array is actually values from getResultList()using the createNativeQuerymethod to query an Oracle database.
我知道原因是因为我试图用 Object[] 数组中的值而不是 List 来填充数据表。Object[] 数组实际上是getResultList()使用createNativeQuery方法查询 Oracle 数据库的值。
My question is: How do I convert the values in the Object[] array into a List so that the datatable is populated?
我的问题是:如何将 Object[] 数组中的值转换为 List 以便填充数据表?
I have tried using the Arrays.toList() method, and then iterating through that adding the values to a list, but I understand that that simply returns a fixed size list of the backed array. It does not return a List of values.
我曾尝试使用 Arrays.toList() 方法,然后遍历该方法将值添加到列表中,但我知道这只是返回支持数组的固定大小列表。它不返回值列表。
Any help would be greatly appreciated. My apologies if I have not provided enough detail. Thank you.
任何帮助将不胜感激。如果我没有提供足够的细节,我深表歉意。谢谢你。
回答by GoGoLander
Try with ArrayList:
尝试使用 ArrayList:
ArrayList<Object> list = new ArrayList(Arrays.asList(yourArray));
and then add whatever element you want to the list using
然后使用任何你想要的元素添加到列表中
list.add(newItem)
回答by stg
The getResultList
method do return a List<Object[]>
if you are for example querying multiple entities and the result cannot be mapped automatically onto an existing entity class.
例如,如果您要查询多个实体并且结果无法自动映射到现有实体类,则该getResultList
方法会返回 a List<Object[]>
。
To avoid this you may consider a constructor expression in your JPA query like this:
为避免这种情况,您可以考虑在 JPA 查询中使用构造函数表达式,如下所示:
Query query = getEntityManager()
.createQuery("SELECT NEW my.Foo(a.id, b.somethingelse) FROM --- ");
The getResultList()
method now will return a List<Foo>
and you do not run into the problem that you have mentioned.
该getResultList()
方法现在将返回 aList<Foo>
并且您不会遇到您提到的问题。