d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > String Array Question
Add Reply New Topic New Poll
Member
Posts: 31,313
Joined: Mar 25 2009
Gold: 0.00
Jul 6 2019 03:41pm
Code
String sentence = "one day in the year of the fox";
String[] arrayWords = sentence.split(" ");




I thought arrays had to specify how many elements to use? But in this code it's not....
Member
Posts: 4,031
Joined: Dec 4 2018
Gold: 56,369.16
Jul 7 2019 04:09am
Code
String[] arrayWords = sentence.split(" ");


this is an array declaration and initialization. If you initialize an array to some value, you do not need to explicitly define how many elements to use (i.e the size of the array) because the compiler is able to do it.
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Jul 7 2019 06:58am
Number of elements in this array is specified by a number of tokens delimited by String#split() (in this case, the delimiter is a whitespace - " ").
After creation of an array, it has static size (in contrast to dynamic size structures, like e.g. ArrayList).
Member
Posts: 31,313
Joined: Mar 25 2009
Gold: 0.00
Jul 7 2019 11:59am
gotchya ty guys :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll