на самом деле имеет специальную опцию для этого:
filter(None, sequence)
Он будет отфильтровывать все элементы, которые оцениваются как False. Нет необходимости использовать фактические вызываемые здесь такие, как bool, len и т. Д.
Это одинаково быстро, как map (bool, ...)
Да, .length
(похоже на свойство, а не на метод):
String[] array = new String[10];
int size = array.length;
Массивы - это объекты, и у них есть поле длины.
String [] haha = {"olle", "bulle"};
haha.length будет 2
В java есть поле длины
, которое вы можете использовать в любом массиве, чтобы узнать его размер:
String[] s = new String[10];
System.out.println(s.length);
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.
Не совсем ответ на ваш вопрос, но если вы хотите иметь что-то вроде массива, который может увеличиваться и уменьшаться, вам не следует использовать массив в java. Вероятно, лучше всего будет использовать ArrayList или другую реализацию List.
Затем вы можете вызвать size () для получения его размера.
конечное свойство array.length
это общедоступное и конечное свойство. Это окончательно, потому что массивы в Java неизменны по размеру (но изменяются по значению элемента)
array.length
На самом деле это последний член массива, а не метод.
Если вы хотите, чтобы функция выполняла это
Object array = new String[10];
int size = Array.getlength(array);
Это может быть полезно, если вы не знаете, какой у вас тип массива, например int [], byte [] или Object [].