5

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>
peterh
  • 11,875
  • 18
  • 85
  • 108
elia
  • 83
  • 1
  • 1
  • 5
  • 3
    `array.length`? Or did I miss something...I'm not sure the coffee is working anymore – codeMagic Sep 10 '13 at 20:50
  • Are you wanting the amount of arrays you have? as in, Red, Blue, Orange = 3 strings... or 13 characters – NodeDad Sep 10 '13 at 20:55
  • @CaseyB : I disagree that this is in any way a duplicate of the question you linked. The OP of that question is asking about the philosophical decisions behind why the original Java developers didn't provide an encapsulated 'getter' method to return the size of an array. The OP here is simply asking how to find out the size of his/her array. – Squonk Sep 10 '13 at 21:25

4 Answers4

15

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);
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • tnx, myList = Arrays.toList(stringArray); has error:The method toList(String[]) is undefined for the type Arrays – elia Sep 10 '13 at 21:07
  • And remember that length returns the NUMBER of elements and NOT the last element position like it's the case in some other languages. {"foo", "bar"} has a length of 2 and not 1. – FlorianB Sep 22 '16 at 22:28
2

Look at this post and understand the difference between array and Arraylist

Where is array's length property defined?

To answer your question, you can get the size of the array by calling .length on it.

array.length

You should also look at this to learn more about arrays.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Community
  • 1
  • 1
Naveed
  • 2,942
  • 2
  • 25
  • 58
0

It depends on what you mean by the size of the items. you could use array.length() or array.size(), so for example maybe something like

Integer arrayLength = (Integer) array.length();

or

Integer arrayLength = (Integer) array.size();

i would imagine...

for more on how size() and length() are different please refer to count vs length vs size in a collection

Community
  • 1
  • 1
NodeDad
  • 1,519
  • 2
  • 19
  • 48
0

In String There are two way of geting the length of String:-

String s; 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   s.length();
}
Xavi López
  • 27,550
  • 11
  • 97
  • 161
Amitsharma
  • 1,577
  • 1
  • 17
  • 29