I have a text file, I use this code to split and process this content:
String[] array = s.split(",");
How I can get size of this array?
The text file filling dynamic, and I don't know size of the items.
I can get the size of ArrayList<string>
, but this object is unuseable here.
public ArrayList<String> myList = new ArrayList<String>();
myList =s.split(",");//error:cannot convert from String[] to ArrayList<String>
We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);
Using sizeof() function to Find Array Length in C++ The sizeof() operator in C++ returns the size of the passed variable or data in bytes.
Unlike the String and ArrayList, Java arrays do not have a size() or length() method, only a length property.
If you want the amount of elements in this array String[] array = s.split(",");
just do:
array.length
To convert this array to a list do this:
public ArrayList<String> myList = new ArrayList<String>();
String[] array = s.split(",");
myList = Arrays.asList(array);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With