简体   繁体   中英

List of fixed size Arrays in Java?

Is it possible to create a List of arrays(with fixed size) in Java?

I have tried these methods and they both give me a syntax error:

List<int[]> failedParameters = new ArrayList<int[3]>();
List<int[]> failedParameters = new ArrayList<new int[3]>();

What am I doing wrong?

No, you can't specify the array length as a Java type . However, you can wrap arrays in your own type. I'll let you decide if that is really practical (in case you have dozens of array lengths that you want to support):

class Int3 {
    // Use final to indicate that the array length will remain unchanged
    final int[] array = new int[3];
}

List<Int3> failedParameters = new ArrayList<Int3>();

Of course, if you go this far, why not just create an int-tuple of degree 3?

class Int3 {
    int v1;
    int v2;
    int v3;
}

List<Int3> failedParameters = new ArrayList<Int3>();

You can use this method. It will not allow you to change the collection size (no add()/remove() operations).

List<> unmodiffableList = Collections.unmodifiableList(oListeRet);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM