Java – is the first element of an array returned using split always safe?
•
Java
I'm sure the answer is yes, but I just want to make sure that no non empty string (whatever it contains) will return anything except a valid string as the first member of the returned array Split
Put it another way
String foo = ""; // or "something" or "a b c" or any valid string at all String[] bar = foo.split(",")[0];
My understanding is that bar will never be null and the assignment line cannot fail If no delimiter is found in the string, it returns only foo as the first element of the returned array
Solution
No, it may fail
If foo =, ", it cannot pass arrayindexoutofbound
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码