[英]How can I convert from java ArrayList object to real value
I'm trying to get a value from the databae. 我正在尝试从数据库中获取价值。
My Database query: 我的数据库查询:
String GroupID1="select idCompanies from companies where Company_Name='ACME';";
here I'm calling to a javabeans which give back an ArrayLIst with one element 在这里,我正在调用一个javabeans,它返回一个带有一个元素的ArrayLIst
ArrayList<?> IdGroup1=oper.getList(GroupID1);
then, I print the result: 然后,我打印结果:
System.out.println(IdGroup1);
The query works fine, however I'm getting as a result: 查询工作正常,但是结果如下:
[javabeans.ListOneElement@609f6e68]
Instead of the real value. 而不是真正的价值。 How can I convert the java object to the real value?
如何将java对象转换为实际值?
您正在打印ArrayList
对象IdGroup1
,您需要进行迭代以获取alues
This code will retrieve the first (and only) item from the list: 此代码将从列表中检索第一个(也是唯一一个)项目:
System.out.println(IdGroup1.get(0).toString());
Adding the following will prevent a nullPointerException: 添加以下内容将防止nullPointerException:
if (!IdGroup1.isEmpty())
System.out.println(IdGroup1.get(0).toString());
-Added .toString()
to get the value of the object -添加了
.toString()
以获取对象的值
Consider what type of Object oper.getList(GroupID1)
will return. 考虑哪种类型的对象
oper.getList(GroupID1)
将返回。
You need to accommodate for whatever object that is and then convert it to String. 您需要容纳任何对象,然后将其转换为String。
You need to: 你需要:
Unpackage your list (that is a list contains, and is expected by java to possibly contain multiple objects, so it doesn't automatically 'unpack' it for you if you have a list of 1 object) 解压缩列表(该列表包含一个列表,java希望该列表可能包含多个对象,因此如果列表中包含1个对象,它不会自动为您“解包”)
Extract your string. 提取您的字符串。 Here java might cleverly convert a number (int, float, etc. ) to a string for you.
在这里,java可能会为您巧妙地将数字(int,float等)转换为字符串。
For part two, look at what you expect the object to be by finding the JavaDocs for whatever package is handling your database queries. 对于第二部分,通过查找处理数据库查询的任何程序包的JavaDocs,来查看对象的期望值。 Then see how to extract your string.
然后查看如何提取您的字符串。
It might be as simple as System.out.println(IdGroup1.get(0).toString());
它可能像
System.out.println(IdGroup1.get(0).toString());
一样简单System.out.println(IdGroup1.get(0).toString());
Where get(0)
unpackages the object from the list, and toString()
extracts the string. 其中
get(0)
从列表中解包对象,而toString()
提取字符串。
If you still get back javabeans.ListOneElement@41ccdc4d
try other methods to extract your string.... toValue()
perhaps? 如果仍然返回
javabeans.ListOneElement@41ccdc4d
尝试其他方法来提取您的字符串。... toValue()
也许? It depends on the packages you're using. 这取决于您使用的软件包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.