Question : Java Data Array Sorting Advice Needed

Hi,
   Hopefully someone can offer me some advice of the best practice for the following coding issue I have come across.

  I have a CheckBoxTree which I can populate fine, but I need to be able to sync the tree with a data array to keep track of which checkboxes are checked, and which are not.

  I have set up several classes for this for Accounts, Products, etc. using classes, so for instance, for Products I have;

public class ProductsSQLConsolidated {
    boolean checked;
    Integer productCode;
}

as a single class file, and in my main code I use
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
private void    allocateConsolidationProducts() {
    int tmpProduct = -1;

    consolProducts[0] = new ProductsSQLConsolidated();

    try {
        ResultSet rs = sqlStatement.executeQuery("SELECT CODE FROM PRODUCTS ORDER BY CODE ASC");

        int i =1;
        while (rs.next()) {
            consolProducts[i] = new ProductsSQLConsolidated();

            tmpProduct = Integer.parseInt(rs.getString("CODE"));

            consolProducts[i].checked = false;
            consolProducts[i].productCode = tmpProduct;
            i++;
        }

        numberProducts = i;
        rs.close();
    } catch (SQLException se){
            System.out.println("Products SQL code does not execute.");
   }
}


Now this works fine, but the only way I can find a product is to scroll through each array element looking for a match on the product code, as the product codes are not in order (the list is in the order of the Product Description), but it doesnt matter if I then sort this array into Product Code order as its used purely for tracking and not displaying the screen.

So, my questions are;

1) Should I sort the array into Product Order once it is populated?
2) Should I re-run the same query which produced the original results but produce the results in product code order and use this to populate the array?
3) What is the quickest way to find a product code in the array list? Is there a standard function which will index the array for me? or do I really need to have the product codes in a set order and then use a binary search to find if the product codes exists?
4) If anyone can think of a better way to produce the same results, please share :)

Regards

Answer : Java Data Array Sorting Advice Needed

It's probably easier just to keep a Map<JCheckBox, ProductsSQLConsolidated> so a click on a checkbox can go straight to the right product
Random Solutions  
 
programming4us programming4us