Cloning java ArrayList and preventing it from modifications
Posted
by user222164
on Stack Overflow
See other posts from Stack Overflow
or by user222164
Published on 2010-04-06T13:04:00Z
Indexed on
2010/04/06
13:13 UTC
Read the original article
Hit count: 351
I have a data structure like a database Rowset, which has got Rows and Rows have Columns. I need to initialize a Columns with null values, current code is to loop thru each column for a row and initialize values to NULL. Which is very inefficient if you have 100s or rows and 10s of column.
So instead I am keeping a initialized ArrayList of columns are RowSet level, and then doing a clone of this Arraylist for individual rows, as I believe clone() is faster than looping thru each element.
row.columnsValues = rowsset.NullArrayList.clone()
Problem with this is NullArrayList can be accidentally modified after being cloned, thus sacrificing the integrity of ArrayList at RowSet level, to prevent I am doing 3 things
1) Delcaring ArrayList as final 2) Any elements I insert are final or null 3) Methods thurough this arrayList are passed to other arrays are declared a final.
Sounds like a plan, do you see any holes ?
© Stack Overflow or respective owner