문제

public void addProductList(ArrayList<ViewOrderProduct> globalOrderProductList) {

    this.productOrderList =  (ArrayList<ViewOrderProduct>)globalOrderProductList.clone();
}

This gives me the warning:

Type safety: Unchecked cast from Object to ArrayList

I know that I can simply solve the problem adding @SuppressWarnings("unchecked").

But I want understand the problem. I can be sure that all goes good if I add the suppressWarnings? Is there another solution for this warning?

도움이 되었습니까?

해결책

clone() returns Object by default, the correct way to clone an ArrayList is to use the appropriate constructor:

this.productOrderList =  new ArrayList<ViewOrderProduct>(globalOrderProductList);

Edit: The preferred way is to use the appropriate constructor, and both methods only return a shallow copy anyways.

Edit: And there's no other way I'm aware of, to remove the warning using clone() without a SuppressWarning.

다른 팁

arraylist is wrapper over Object[], anything inside stored as Object, it does not matter what constructor is used result will be same.

as stated above, to evoid warning you shoud use constructor public ArrayList(Collection c) {}

eg new ArrayList(globalOrderProductList);

this will return copy of content without warnings.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top