I'm making a scoreboard for a simple word-game. The easiest way for this seems to be a JTable. I need both column headers and row headers, I believe this is achievable in JTable? For each game, the column headers should represent a random selection of predetermined categories, and the row headers should represent a random selection of characters from A to Z.

I started out making one enum for characters and one for categories. The plan was to take a random selection from it into an array and pass it to a class extending the AbstractTableModel. Then I figured it would be easier to just to create variables for the char and string arrays. The idea was to create a sub array from these with a random selection (5 characters and 5 strings). But I struggle with the creation of a random array. I figured I could use the Collections.shuffle and then Arrays.copyOfRange but this leads to a lot of conversion back and forth between List and array. Could anyone point me in the right direction here? I would like to do this as efficient and memory optimized as possible.

Please let me know if you need further details.


If you are just selecting 5 random elements from a collection of 26 then frankly you are wasting your time worrying about efficiency, either for CPU or memory.
Maybe with optimal coding you could save a microsecond of CPU, and a few hundred bytes of temporary memory. Unless you plan to do this millions of times in rapid succession you can safely not worry about efficiency.
Just code it in any simple, obvious, understandable way.

Your plans for JTable and extending AbstractTableModel (or maybe DefaultTableModel) seem entirely sensible and practical.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.