gpt4 book ai didi

java - 如何使用 boja 类将一个列表添加到另一个列表

转载 作者:行者123 更新时间:2023-12-02 03:45:41 26 4
gpt4 key购买 nike

我必须分别运行两个查询并添加到单个列表中,如下代码所示,

    List<BojaClass> results1 = new ArrayList<>();
List<BojaClass> results2 = new ArrayList<>();
String sql1 = "my query 1";
String sql2 = "my query 2";
results1 = jdbcTemplate.query(sql1, new Object[]{1,2}, new BeanPropertyRowMapper<BojaClass>(BojaClass.class));
results2 = jdbcTemplate.query(sql2, new Object[]{1,2}, new BeanPropertyRowMapper<BojaClass>(BojaClass.class));
results1.add((BojaClass) results2);

但我收到异常“java.lang.ClassCastException:java.util.ArrayList无法转换为com.myclass.BojaClass”,并且results2值未添加到results1中。

我哪里做错了?

最佳答案

您正在使用 List 接口(interface)的 add 方法,该方法仅将单个元素添加到列表中。使用 addAll 方法来添加另一个列表。

boolean java.util.List.add(E e)

Appends the specified element to the end of this list (optional operation).

boolean java.util.List.addAll(Collection c)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

关于java - 如何使用 boja 类将一个列表添加到另一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36333419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com