javasqllistfixed-length-record

Java: How to initialize a fixed length List of List?


I have a list of String[] that contains some data returned from a database

List<String[]> x;

Now for each "x" I have to store its relative Ys. (Imagine that "x" contains the elements returned from a "SELECT DISTINCT xColumn1, xColumn2 FROM table" and now for each element I have to store the data from another query).

Y's elements are going to be stored here:

List<List<String[]>> yElements;

I've read that I can declare a fixed length list, but I don't know how. This is what I have done:

yElements = Arrays.asList(new List<String[]>[x.sixe()]);

Netbeans told me "generic array creation" error.

I also want to tell you that I MUST have a fixed lenght list to store a List of String[ ] in a specific index of this List of Lists. (If you can show me how to do with an array it would be great, too!) Thanks.


Solution

  • If you want your lists to be absolutely fixed size, you could use the native arrays. They can be multi-dimensional so for example you can have String[][] x or `String[][][] y'.

    Honestly however, your approach is a bit confusing and not that crisp from a design point of view.

    Why not, similarly to as was suggested in the comment, have an object which has both columns (xColumn1, xColumn2), and then have the Y elements in a separate object, which can then be associated with the first one?

    so:

    class XDetails
    {
       String xColumn1;
       String xColumn2;
    
       YDetails relatedY;
       ...
    }
    
    class YDetails
    {
       ... fields of Y ...
    
    }
    

    Then you can have an array or List<XDetails>