Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the size of a string array?

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>
like image 781
elia Avatar asked Sep 10 '13 20:09

elia


People also ask

How do you get size of an array?

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]);

How do I get the length of a string array in C++?

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.

Does size () work on arrays?

Unlike the String and ArrayList, Java arrays do not have a size() or length() method, only a length property.


1 Answers

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);
like image 92
Steve Benett Avatar answered Sep 23 '22 06:09

Steve Benett