Reputation: 4054
Just a thought question here. In C++, I could do the following:
vector<vector<string> > data;
// add data into data
//..
data[0].push_back( "somedata" );
And I would expect somedata
to get written to the vector array because the []
notation gives me access to the object by reference. What about in Java? If I:
List<List<String>> data = new ArrayList<List<String>>();
// add data into data
//..
data.get(0).add( "somedata" );
Would this actually write somedata
into the data object? Or would it create a new copy of the element at data(0), add somedata
to that, and then that object disappears into GC sometime down the line?
Upvotes: 1
Views: 1915
Reputation: 122429
You must first understand that List
, String
, etc. in Java, these types are reference types. Their values are references, which are pointers to objects. Thus, List<List<String>>
in Java would be most equivalent to vector<vector<string *> *> *
in C++. You cannot have a direct "object value" in Java like you can in C++; objects are always hidden behind a pointer.
So to answer your question, yes, the Java code you show works, but for very different reasons. In your Java code, you have a list of references (pointers). You want to modify stuff in the object that is pointed to by one of these pointers, but you do not need to change the pointer itself. Thus, there is no need to return the element by reference. It is sufficient to return the element (a pointer) by value.
Your question "Would this actually write somedata into the data object?" is kind of ambiguous. The code modifies the object pointed to by the first element of the list. Whether this constitutes modifying the list object itself depends on what you consider to be "part of" an object. As explained earlier, the list object contains a collection of pointers to objects. Should the objects pointed to by these pointers to be considered "part of" the list object? There could be many pointers to the same object. So if you consider it to be a part of the container, then what happens when there are pointers to the same object in multiple containers, is the object then part of all of these containers at the same time?
The answer to "Or would it create a new copy of the element at data(0)" is, it creates a copy of the pointer that is the first element. It does not create a copy of the object that the pointer points to.
Upvotes: 2
Reputation: 86764
Almost. The pattern you need is:
List<List<String>> data = new ArrayList<List<String>>();
data.add(new List<String>());
data.get(0).add( "somedata" );
The first line creates only the "outer" List of Lists; you have to populate it with one or more Lists (of Strings) before you can add data to the inner lists.
Upvotes: 1
Reputation: 8278
ArrayList is a List backed-up by array (in order to enable random access) the list stores references to real elements so when you add a new element as you mentioned, the reference to it will be added to the ArrayList (and the backing Array will point to this List element).
Upvotes: 3