• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 dimensional Array vs HashMap  RSS feed

 
Jay Brass
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to add data from a resultSet to a JComboBox. It always has to be in the order that the database returns it to me. A minus for HashMap and a Plus for Array. I can get the number of columns from the ResultSetMetaData and I can get the row count by setting the resultset to rs.last rs.getRow() and resetting it back to rs.beforeFirst to iterate over it. This gives me the 2 values for my 2 dimensional array.
new Object[rowcount][columnCount] So if the database people add more items I don't have to change my code. I know an ArrayList is variable length but it's slow.

An array will help me create the model for the JComboBox. In the database the items are stored with a column that specifies the numeric ranking of the item and a column for the text description to be displayed. The numeric ranking starts with -1 and a JComboBox index starts at 0. The problem currently is when a user chooses an item and I get the selectedIndex of the JComboBox, I'm off by 1 when I do the look up in the database. I could just subtract one from the selectedIndex when I get it but I was hoping for a better and more dynamic solution in case the database people add a -2 item. I don't think there will ever be null values but I know better than to assume this.

As I understand them:
Arrays - First class object. An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed

HashMap - This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

My first thought was to use a HashMap since it would be easier to get the key based on the value selected (JComboBox.getSelectedItem) and pass the key to the database which would always give me the correct results. Should I sort the Hashmap first, before creating the JComboBox model? Or should I iterate over the 2D array and create a new array from the descriptions that it holds?
On the Object oriented front you're going to tell me to create a class to hold my values returned from the database where one field of that class is the numeric ranking and the other is the description. I think.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Brass wrote: . . . This gives me the 2 values for my 2 dimensional array. . . .
There is no such thing as a 2D array. Only an array of arrays. An array where arr[i][0] and arr[i][1] are paired sounds like an abomination against object‑orientation. If those two values have some relationship to each other, you should encapsulate them both into one object. Then you have an array of that type.
 
Jay Brass
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm probably over thinking the problem. I figured creating a whole class to hold these was overkill. That's why I thought HashMap until I realized I needed them in a certain order. From there I moved to multi dimensional arrays or as you put it an array of arrays. I think I could still use a TreeMap for this though instead of a new class.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Brass wrote:I think I could still use a TreeMap for this though instead of a new class.


Why? "Creating a whole class" is not overkill when you have two or more pieces of data that go together as pieces of some domain concept. That's part of what classes are for. It would have taken less typing and less time to do it than what you posted here.

A big part of programming is finding the right tool for the job--finding the idiom that clearly and correctly expresses your design intent. What is NOT a big part of programming is saving a few bytes or keystrokes.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you're not over-thinking the problem, you're just mis-thinking it. I don't know how you're creating your JComboBox but let's assume you're using the constructor "JComboBox(E[])". In other words, you are creating an array of combo-box entries. Each of those entries should be of the same type.

So you don't need a class to hold all of the entries, you need a class to hold one of them. The attributes of the class would be the String to be displayed by the combo-box and whatever else you need. I don't know whether you need the numeric ranking or not, perhaps you can just order by that when extracting from the database and then load the array in the correct order.
 
Jay Brass
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thanks to you all for your replies. It made me think a little more about what I was trying to do. The solution I have come up with is this:
This works:
sourceComboBox.setModel(dba.getSource());
calls the class to get the resultset from the database

in the database class:
public DefaultComboBoxModel getSource() {
....
I still have to do the trick to get the number of rows returned with the cursor
in order to create my arrays
.....

builder is an object of the container class that will hold the array of the numeric ranking, in this case an array of results from the database. That's all I need because the other array is used ot create the DefaultComboBoxModel which is the return type of this method.
The Builder class uses the builder design pattern to hold dynamic data throughout the application. I get an instance to the builder class and in my class that sets the model to the JComboBox:
builder.source(builder.build().dataSources()[sourceComboBox.getSelectedIndex()]);

It does what I need it to do and I don't need to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!