Java dynamic array sizes?

前端 未结 18 2461
星月不相逢
星月不相逢 2020-11-22 04:37

I have a class - xClass, that I want to load into an array of xClass so I the declaration:

xClass mysclass[] = new xClass[10];
myclass[0] = new xClass();
my         


        
18条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 05:17

    Since ArrayList takes to much memory when I need array of primitive types, I prefer using IntStream.builder() for creating int array (You can also use LongStream and DoubleStream builders).

    Example:

    Builder builder = IntStream.builder();
    int arraySize = new Random().nextInt();
    for(int i = 0; i

    Note: available since Java 8.

提交回复
热议问题