We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am making a function to remove any lines that start with the character # from an array of strings. I am getting an error though. cannot invoke size() on an array of type String[]. This makes sense but it also doesnt. How can i get the number of elements in an array of strings?
for(int i = 0; i < elements.size(); i++)
{
if(elements[i].sub(0,1) = "#")
{
elements.remove(i);
}
}
println(elements);
Answers
elements.length
is the size of the array, how many different strings you have.elements[i].size()
would be the size of the string at positioni
in your array.The size of the array is
elements.length
but it won't do you any good because there is an error on line 3 and in line 5 there is no such method remove() for an array.
This code will do what you want and is easy enough to follow what happens.
Arrays, alongside String, Object and all of the 8 wrappers for the corresponding primitive types,
got some special treatments from the Java language. And among them, arrays got most privileges! ^:)^