简体   繁体   English

如何获取数据并迭代作为对象返回的arraylist?

[英]How to obtain data and iterate over arraylist returned as an object?

In my app I created an ArrayList in some servlet which was assigned using request.setAttribute() as 'testList'.在我的应用程序中,我在使用 request.setAttribute() 作为“testList”分配的某个 servlet 中创建了一个 ArrayList。 Then request was forwarded to jsp page.然后请求被转发到jsp页面。 Inside jsp page I want to retrieve whole ArrayList and iterate over it.在 jsp 页面中,我想检索整个 ArrayList 并对其进行迭代。

I used getAttribute(), but after that I cannot retrieve an ArrayList from that object.我使用了 getAttribute(),但之后我无法从该对象中检索 ArrayList。

An ArrayList contains objects TestObject created by me in another class (which was imported properly). ArrayList 包含我在另一个类(已正确导入)中创建的对象 TestObject。 I would like to get access to it by iteration over ArrayList.我想通过遍历 ArrayList 来访问它。

Object testList = request.getAttribute("testList");
ArrayList<TestObject> localList = new ArrayList<TestObject>();
localList = testList;
//Type mismatch: cannot convert from Object to ArrayList<TestObject>

What is a best practice to obtain data from Object to ArrayList?从 Object 获取数据到 ArrayList 的最佳实践是什么?

Should I use a cast (IDE warns: Type safety: Unchecked cast from Object to ArrayList)?我应该使用强制转换(IDE 警告:类型安全:未检查的从 Object 到 ArrayList 的强制转换)? Or generics?还是泛型? Or... ?或者... ?

What you need is a cast.你需要的是一个演员。 The defensive solution checks that the type is correct before casting:防御解决方案在转换之前检查类型是否正确:

    Object testList = request.getAttribute("testList");
    if (testList instanceof Collection) {
        for (Object obj : (Collection<?>) testList) {
            if (obj instanceof TestObject) {
                TestObject currentTestObject = (TestObject) obj;
                // Do something with currentTestObject
            }
        }
    }

To iterate over the list returned from request.getAttribute you only need to assume it is a Collection , not necessarily ArrayList .要迭代从request.getAttribute返回的列表,您只需假设它是一个Collection ,而不一定是ArrayList Since ArrayList implements the Collection interface the above code will work with ArrayList and with other collection types too.由于ArrayList实现了Collection接口,因此上面的代码也适用于ArrayList和其他集合类型。 If you only want it to work with List or only ArrayList , just use one of those types instead of Collection in the code.如果您只希望它与ListArrayList使用,只需在代码中使用这些类型之一而不是Collection

Consider adding else parts to the if statements and issue a log message and do some other appropriate action if a runtime type wasn't as expected.如果运行时类型不符合预期,请考虑向if语句添加else部分并发出日志消息并执行一些其他适当的操作。

If you are sure you always get an ArrayList<TestObject> , you may omit the checks:如果你确定你总是得到一个ArrayList<TestObject> ,你可以省略检查:

    Collection<TestObject> testList = (Collection<TestObject>) request.getAttribute("testList");
    for (TestObject currentTestObject : testList) {
        // Do something with currentTestObject
    }

This is somewhat simpler.这个稍微简单一些。

Link: Object Type Casting in Java链接: Java 中的对象类型转换

You can pass your object to your ArrayList constructor :您可以将对象传递给 ArrayList 构造函数:

Object testList = request.getAttribute("testList");
ArrayList<TestObject> localList = new ArrayList<TestObject>(testList);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM