0

I need to have a fixed size array with the length of 10, consisting of double values as they used as a record so this should have efficient structure.

I wonder if there is any fixed length collection in fastutil or simply I can use a double[10] array instead of fastutil?

c0der
  • 18,467
  • 6
  • 33
  • 65
Luckylukee
  • 575
  • 2
  • 9
  • 27
  • *"need to have a fixed size array"* In Java, all arrays are fixed size. --- *"consisting of double values"* So an array of `double`? --- *"should have efficient structure"* An array is very efficient. --- *"I can use a double[10] array instead of fastutil?"* Yes. Why did you think you couldn't? Did you even *try*? – Andreas Feb 04 '18 at 01:33
  • How do you define `efficient structure`? It's mainly based on your use case. However, as the size is only 10, any basic operation (e.g. insert, delete, search, etc) is O(1) only. Hence, it's better to use double[10] only. – sayboras Feb 04 '18 at 01:33

1 Answers1

2

Array is the way to go. If you must have a fixed size collection you can create one that is backed by array and its size can not be changed :

Integer[] ints = {0,1,2,3,4,5,6,7,8,9};
List<Integer> listBackedByArray = Arrays.asList(ints); //fixed size list
listBackedByArray.add(10);// will produce UnsupportedOperationException
c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1
    And you can still use `listBackedByArray.set(2, 4)`, so it's a proper `Collection` (and not like you can't change it at all). You do have to be careful because of writethrough from the original array (not sure how that is handled actually). – daniu Mar 16 '18 at 07:48