I have a String data type adjacency matrix in Java:
String[][] A;
I want to read my adjacency matrix A into a MySQL table. The problem is I never know how many rows/columns I will need (nor would I want to create all the columns either). I think the trick to solving this problem is creating columns on the fly. How do I add columns “on the fly?”
Once I can add columns on the fly, I could then read in the matrix row by row.
- I want to be able to view the data as a matrix.
All help is greatly appreciated!
Instead of going through the trouble of setting up variable columns, create a simple database table A:
This gives you the flexibility to easily avoid empty entries and you have one database table row for every array item.
You have two options:
For option 1, this code will provide any element:
For option 2, the content of the requested element at row,col is found like this:
It is obvious that option 1 is accessing elements faster. Option 2 saves database space.
PS: If you have an array of arrays of varying length option 1 will not work.
colNumber is the fixed number of columns of your String array.