Skip to content
Advertisement

Creating a Matrix with ArrayLists

I want to create an ArrayList-matrix with n-rows and m-columns, for example

JavaScript

I’ve already written a code for creating such a matrix but my code doesn’t display the values when I clear the list containing the column-data. Here is my

JavaScript

Is there any possibility to clear the contents of intList without clearing the contents of mainList??

Advertisement

Answer

When calling mainList.add(intList); you are adding a reference pointing to the intList object into your mainList rather than copying the values. You will have to create one instance of “intList” per row, and not clear anything.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement