【问题标题】:How to cast an Object to Arraylist如何将对象转换为 Arraylist
【发布时间】:2016-01-24 09:04:28
【问题描述】:

我需要将一个对象转换为 Arraylist:

<% Arraylist<Products> mypr = sesion.getAttribute("products");  %>
            <table class="table table-condensed">
                <tr>
                    <th>Id</th>
                    <th>Category</th>
                    <th>Model</th>
                    <th>Stock</th>
                    <th>Price</th>
                </tr>
                <%
                    for (Products xx : mypr) {
                        out.println(xx.toString() + "<br>");
                    }
                %>

【问题讨论】:

  • 我不能使用我的“for”,因为它是一个对象而不是一个数组列表
  • session.getAttribute("products");
  • 原来是ArrayList吗?
  • 不,来自另一个 Servlet 的对象
  • 显示来自该其他 servlet 的setAttribute() 代码

标签: java object arraylist


【解决方案1】:

假设它是一个ArrayList,你可以转换它(我推荐List 接口之类的)

List<Products> mypr = (List<Products>) sesion.getAttribute("products");

【讨论】:

  • misDatos = ArrayList(session.getAttribute("products")); %>
  • @SebastianAbiel 上面的括号很重要。
猜你喜欢
  • 2014-08-28
  • 1970-01-01
  • 2019-08-17
  • 2014-11-10
  • 1970-01-01
  • 2020-10-29
  • 2013-10-16
  • 1970-01-01
  • 2011-02-14
相关资源
最近更新 更多