Question

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.

Était-ce utile?

La 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>

Autres conseils

You cannot create an instance of an array of a generic type using new.

One alternative is this:

import java.lang.reflect.*;
import java.util.*;

public class Test
{
    public static void main( String args[] )
    {
        List<String[]> x = ...;
        List<List<String[]>> yElements = Arrays.asList(
                (List<String[]>[])Array.newInstance(List.class, x.size()));
    }
}

Related question: How to create a generic array in Java?. (The example there is about creating an instance of an array of a generic type parameter ... but the same approach applies here.)


However, I think that this whole question is based on an incorrect assumption.

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.

You don't HAVE TO have a fixed sized list. From the computational perspective, your code would work just fine with a non-fixed sized list ... provided that you don't add or remove list elements. (In fact, using Arrays.asList to wrap an array won't stop some other code trying to add / remove elements ...) Anyway ... if you just make the implementation type ArrayList<ArrayList<ArrayList<String>>>, then the generic array creation problem goes away.

In addition, I suspect that it is incorrect to use x.size() as the size of yElements. The size of yElements probably should be determined by the number of x instances there are going to be, not the size of a given x instance.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top