Differences when assigned as clear and new in Java arrayList

Asked 2 years ago, Updated 2 years ago, 90 views

Hello, I used clear while working, but my friend who didn't have the price reallocated it to new, so it came out properly

I was going to find out the difference, but I have no idea, so I'm asking you a question.

The first code is simply as follows:

@Override public List < CloudShoppingHisVO > selectViewGoodsList(ParamMap viewGoodsMap) throws Exception { List < CloudShoppingHisVO > ShoppingHisList = mypageDAO.selectViewGoodsList(viewGoodsMap); //Query to select a list (it is a friend who can be retrun) ParamMap param = new ParamMap(); ParamMap imageMap = new ParamMap(); List < VodImageVO > titleImageList = new ArrayList < VodImageVO > (); List < VodImageVO > posterImageList = new ArrayList < VodImageVO > ();

//creating a titleImageList object that is an attribute of the list to be restored

for (CloudShoppingHisVO shoppingHisVO: ShoppingHisList) {
    ... (Code omitted)

    List < VodImageVO > imageList = displayDAO.selectVodImageList(param);

    titleImageList.clear();
    posterImageList.clear();

    //Clear before inserting (for removing the previous value while turning the door)

    for (VodImageVO image: imageList) {
        String type = image.getType();

        if ("10".equals(type)) {
            titleImageList.add(image);
        } } else if ("20".equals(type)) {
            posterImageList.add(image);
        }

    }
    //Set the title ImageList added above to the FOREACH Mundo VO.
    shoppingHisVO.setTitleImageList(titleImageList);
    shoppingHisVO.setPosterImageList(posterImageList);

}

return ShoppingHisList;

} The code using clear is like this. Debugging also confirms that the value fits well.

But if you put BP on the last return...

It's empty like this... So, I regenerated it with new instead of clear function and it worked well..?

But I don't know why this is happening.

It is assigned directly to an object called shoppingHisVO, and all the titleImageList objects are in the VO

If you check the shoppinHisList that goes around the foreach door, it's gone.

But if you use new instead of clear, it goes in normally.

Isn't it worse in terms of performance than clear if you use new? That's why I wrote clear.

Why is he doing this? I'm so curious.

java arraylist

2022-09-20 16:02

1 Answers

Are you a non-major?

Create new memory allocation instance Return reference value..

If clear, do not handle the above function.


2022-09-20 16:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.