Why do arrays need a measurement set during determination?

I'm just wondering why we can't just define something like this:

int[] arr = new int[];
arr[0] = 1;

Like the list, to automatically resize. I want to know why this is not possible, and we must set the size each time as follows:

int[] arr = new int[1];
arr[0] = 1;
+4
source share
5 answers

List<T> resizing is based on creating a new array behind the scenes when necessary.

, . , , . , , , - ... , , . ( , .)

- , , , , . , List<T>, , . , , List, ... .

, , List<T> - , . " ", .

- , ...

+12

, .

, 100 . 1, 100- . , ?

.

+7

", " - , , . , (C, ++, Java - 1), , , , - .

List<T>, , , , ( , ), .

1.8 # :

- , , . , , , type, .

, . , . , . 0 - 1. , , , null .

, , " " - ECMA 334 Spec:

, , , , . , .

1 - #:

# ( "See Sharp" ) - , , - - . # C C, ++ Java .

+2

, , , .

"", List.

. ( ), . .

+1

, .

If you need a collection with dynamic size, there are others, such as the list you talked about.

What you can do is to delay the declaration of the size of the array:

int[] numbers;
numbers = new int[10];  
numbers = new int[20];  
+1
source

Source: https://habr.com/ru/post/1533496/


All Articles