سؤال

I have a method that returns a List. (getListMethod)

Is there functional difference between?

ArrayList myList = (ArrayList) getListMethod();

and

ArrayList myList = new ArrayList(getListMethod());
هل كانت مفيدة؟

المحلول

Yes, there is a difference.

ArrayList myList = (ArrayList) getListMethod();  

creates a new reference to the same ArrayList, while

ArrayList myList = new ArrayList(getListMethod());  

copies the elements of the old list to a new one.

I am assuming here that you know that getListMethod() really returns an ArrayList, otherwise you might run into other problems with the first variant as well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top