简体   繁体   English

如何从Java ArrayList对象转换为实际值

[英]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: 你需要:

  1. 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个对象,它不会自动为您“解包”)

  2. 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. 这取决于您使用的软件包。

如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换ArrayList <Object> 到ArrayList <String> 或ArrayList <Timestamp> ? - How can I convert ArrayList<Object> to ArrayList<String> or ArrayList<Timestamp>? 如何在Java中访问数组列表中Object []中的值 - How can I access a value in an Object[] in an Arraylist in Java 如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream 如何从Java中的arraylist返回特定的对象(索引)? - How can I return a specific object (index) from an arraylist in Java? 如何从ArrayList返回对象? (JAVA) - How can I return an object from a ArrayList? (JAVA) 如何转换 ArrayList<Object> 到 ArrayList<String> ? - How can I convert ArrayList<Object> to ArrayList<String>? 如何从java中的Object ArrayList获取ArrayList值? - How to get ArrayList value from a Object ArrayList in java? 如何转换属性ArrayList <String> 或ArrayList <Integer> 或ArrayList <MYCLASS> 从JSON对象到Java对象? - How to convert an attribute ArrayList<String> or ArrayList<Integer> or ArrayList<MYCLASS> from a JSON object to a java object? 如何将 ArrayList 转换为 JSON Object 以便能够在 ZD52387880E1EA921387A 中迭代它? - How can I convert an ArrayList to JSON Object to be able to iterate through it in Java? 如何从 java 中的另一个 ArrayList 中删除 ArrayList 的元素? - How can I delete an element of an ArrayList from another ArrayList in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM