ArrayList references to objects

前端 未结 2 439
北恋
北恋 2021-01-25 00:44

I\'m a little confused on what it means about ArrayLists holding references to objects. Can someone give me an example on how this is shown in code? Also, can arraylist have an

2条回答
  •  逝去的感伤
    2021-01-25 00:57

    It means instead of copying the object byte-for-byte, a reference to the location of memory where the object is stored is put in the list.

    List objects = new ArrayList();
    Object myObject = new Object();
    objects.add(myObject);
    // objects contains one reference to myObject
    
    objects.get(0).modify();
    // myObject modified
    
    
    

    Note that primitive values (int for example) will be copied.

    提交回复
    热议问题