Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

2d array vs 2d arrayList

807606Apr 23 2007 — edited Apr 25 2007
Hi,

I am working on a project using java swing. I read in a text file that has some data in it (a matrix of numbers - x rows, y columns) and then populate and display a table that has these numbers. This in turn creates another matrix called distanceMatrix.

At this point I have 2 objects... the original matrix (called expressionMatrix) which has X rows and Y columns. This matrix needs to be resized and repopulated every time the user wants to work with a new file.

The other object is called distanceMatrix, which has X rows and X columns. The algorithm that I am about to write calls for this matrix to merge its rows, 1 at a time until there is only 1 row remaining. (so this matrix will change size often).

At this point I have implemented everything with 2d arrays. Every time I need to resize one of the matricies I use the new operator to make a 2d array of a new size. For example...here is some code from MyTableModel.java (which is the object that stores the expressionMatrix mentioned above)...
  // This method is called every time the user trys to load a new file
    public void resizeTableMatrix(int rows, int cols) {
        rowNames = new String[rows];
        colNames = new String[cols];
        numRows = rows;
        numCols = cols;
        matrix = new Object[rows][cols];
	fireTableStructureChanged(); 
    }
I have 3 questions I guess...

1. Should I go back and implement expressionMatrix as a 2d arrayList (or some other alternative) instead of a 2d array...then I wouldnt be recreating the matrix each time the user wants to work with a new file, but just resizing it.

2. What data structure do you recommend for distanceMatrix. Right now it is a 2d array, but I think that needs to change. Its going to go through a while loop where each iteration merges 2 of the rows, until there is only 1 row left. More specifically, 2 rows are selected to be merged (by some other function that I have written), a new row is inserted into the matrix, containing the merged results, the 2 rows that were merged are deleted from the matrix. (It might be useful to note that it doesn't matter where I put the new merged row. I mean I could change one of the selected rows into the merged row, and then just delete the other selected row...)

3. Can anyone point me on the right track for implementing a 2d arrayList (or an alternative). I'm guessing I would need an ArrayList of ArrayLists. So for example, say my 1st arrayList contained the rows of a matrix, and the 2nd contained the columns. If I want to resize the number of columns for the whole matrix, do i need to loop through each of the rows and change the number of columns to the appropriate number?

I hope this makes sense...please tell me if anything needs further clarification. Sorry, its a lot to read, but I wanted to try to explain my questions well.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2007
Added on Apr 23 2007
3 comments
752 views