Java String array: есть ли размер метода?

Фильтр

на самом деле имеет специальную опцию для этого:

filter(None, sequence)

Он будет отфильтровывать все элементы, которые оцениваются как False. Нет необходимости использовать фактические вызываемые здесь такие, как bool, len и т. Д.

Это одинаково быстро, как map (bool, ...)

147
задан Jops 13 April 2013 в 18:21
поделиться

8 ответов

Да, .length (похоже на свойство, а не на метод):

String[] array = new String[10];
int size = array.length;
274
ответ дан 23 November 2019 в 22:14
поделиться

Массивы - это объекты, и у них есть поле длины.

String [] haha ​​= {"olle", "bulle"};

haha.length будет 2

14
ответ дан 23 November 2019 в 22:14
поделиться

В java есть поле длины , которое вы можете использовать в любом массиве, чтобы узнать его размер:

    String[] s = new String[10];
    System.out.println(s.length);
11
ответ дан 23 November 2019 в 22:14
поделиться

The answer is "All of them". A java array is allocated with a fixed number of element slots. The "length" attribute will tell you how many. That number is immutable for the life of the array. For a resizable equivalent, you need one of the java.util.List classes - where you can use the size() method to find out how many elements are in use.

However, there's "In use" and then there's In Use. In an class object array, you can have element slots whose elements are null objects, so even though they count in the length attribute, but most people's definitions, they're not in use (YMMV, depending on the application). There's no builtin function for returning the null/non-null counts.

List objects have yet another definition of "In Use". To avoid excessive creation/destruction of the underlying storage structures, there's typically some padding in these classes. It's used internally, but isn't counted in the returned size() method. And if you attempt to access those items without expanding the List (via the add methods), you'll get an illegal index exception.

So for Lists, you can have "In Use" for non-null, committed elements, All committed elements (including null elements), or All elements, including the expansion space presently allocated.

5
ответ дан 23 November 2019 в 22:14
поделиться

Не совсем ответ на ваш вопрос, но если вы хотите иметь что-то вроде массива, который может увеличиваться и уменьшаться, вам не следует использовать массив в java. Вероятно, лучше всего будет использовать ArrayList или другую реализацию List.

Затем вы можете вызвать size () для получения его размера.

1
ответ дан 23 November 2019 в 22:14
поделиться

конечное свойство array.length

это общедоступное и конечное свойство. Это окончательно, потому что массивы в Java неизменны по размеру (но изменяются по значению элемента)

15
ответ дан 23 November 2019 в 22:14
поделиться
array.length

На самом деле это последний член массива, а не метод.

44
ответ дан 23 November 2019 в 22:14
поделиться

Если вы хотите, чтобы функция выполняла это

Object array = new String[10];
int size = Array.getlength(array);

Это может быть полезно, если вы не знаете, какой у вас тип массива, например int [], byte [] или Object [].

9
ответ дан 23 November 2019 в 22:14
поделиться
Другие вопросы по тегам:

Похожие вопросы: