tags:

views:

110

answers:

3

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 ?

+6  A: 

You can use Collections.unmodifiableList((List) originalList.clone())

Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists.

Bozho
This is not ok. You can still add something to the unmodifiable list by adding something to the original list.
Fortega
added a call to `clone()`
Bozho
That's better :)
Fortega
+5  A: 

Still you will be able to add new elements to the array.

Why not just use:

row.columnsValues = Collections.unmodifiableList( (ArrayList) rowsset.NullArrayList.clone())

pajton
the clone() method returns an Object, so rowsset.NullArrayList.clone() should be casted to an ArrayList
Fortega
@Fortega thnx, corrected now.
pajton
myList = Collection.unmodifiableList(rowsset.NullArrayList.clone())I need the ability to modify myList elements as followsmyList.set(i, new Object());However I want to prevent, myList.get(i).setXYZ(), when return object is one of the original as a result of clone(). I think way to solve it will be when I create original NullArrayList I need to store objects that are defined as "final"final Object obj = new Object();NullArrayList.add(i, obj);
tech20nn
@user222164: That means you don't want your list to be unmodifiable then but the actual objects within the list and that's a whole another issue albeit easily solveable.
Esko
Thanks everyone. I wanted to protect the original list and its contents, its getting clear in my head. I will post the solution.
tech20nn
A: 

Here is the class whose instance keeps common ArrayList, this list is shared but can not be changed outside this class. Elements within this list also need to protect themselves as a shallow copy is being returned.

public class ArrayListUnmodifiability {


    private ArrayList myList;
    public ArrayListUnmodifiability() {

    myList = new ArrayList(2);      
    MyObj obj = new MyObj("Cannot be changed");
    obj.setValue(1);
    myList.add(0, obj);
    myList.add(1, null);        
}


public ArrayList getList() {
    return  (ArrayList) myList.clone();
}   

}

public class MyObj {

public MyObj(String name) {
    this.name = name; 
}

public String getName() {
    return name;
}

private final String name;

}

tech20nn