The drawbacks of the Java list add

Source: Internet
Author: User

Import Java.util.arraylist;import Java.util.list;class t{Private  intXPrivate  intY Public int GetX() {returnX } Public void SetX(intx) { This. x = x; } Public int GetY() {returnY } Public void sety(intY) { This. y = y; }} Public classMain { Public Static void Main(string[] args) {inti =Ten; List<t>list =NewArraylist<> (); t A =NewT ();//--------------(1)         while(i!=0){//T A = new t (); --------------------(2)A.setx (i); A.sety (i+9);            i--;        List.add (a); } for(i=0;i<9; i++) {System. out. println (i+": "+list.Get(i). GetX ()); }    }}

(1) Condition output

011121314151617181

(2) Output

0101928374655647382

This shows that when the list is added to the object, only the address is added, if the address does not change, the value changes, and there is no change. (Documents are as follows in English and Chinese)

/** * Appends theSpecified element to  the End  ofThisList(Optional * operation). * * <p>lists thatOperation May Place Limitations onWhat * elements May added toThisList. In particular,some* Lists'll refuse toAdd null elements, andOthers'll impose * restrictions on  theType ofElements thatMay is added. List * classes should clearly specifyinchTheir documentation any restrictions * onWhat is elements may added. * * @param e element toBe appended toThisList* @return<tt>true</tt> ( asSpecified by{@link Collection#add})* @throws unsupportedoperationexceptionif  the<tt>add</tt> Operation * is  notSupported byThisList* @throws classcastexceptionif  the class  of  theSpecified element * preventsit  fromBeing added toThisList* @throws NullPointerExceptionif  theSpecified element isNull andThis *List does  notPermit NULL elements * @throws illegalargumentexceptionif some  Property  ofThis element * preventsit  fromBeing added toThisList*/BooleanAdd (e e);/* * Adds the specified element to the end of this list (optional * action). * <p> This is the list of supported actions. * Symbols can be added to the list. In particular, some * lists will reject the addition of NULL elements, and others will apply * to the type restrictions that can be added to the element.     The list * class should explicitly stipulate any restrictions in its documentation * What elements can be added. * * @ parameter e element is added to this list * @ return <tt> true </tt> (specified {@link set # Plus}) * @throws unsupportedoperationexception exception if The action in <tt> add </tt> * does not support this list * @throws classcastexception-If the class specifies an element * does not allow it to be added to this list * @throws Nullpo     Interexception-If the specified element is null and this * list does not allow a null element * @throws IllegalArgumentException-If some property of this element * does not allow it to be added to this list * /

Copyright notice: All brothers, please feel free to reprint, please indicate who is the brother

The drawbacks of the Java list add

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.