I'm using Java 1.7 and I need to convert a String array to int array which contains length of each element in String array.
Reminder: I need to pass length array as argument of method several times and I am not interested to pass String array due to some confidential issue. Thats why I need to store lengths as int array.
For example, I have a String array something as
String[] names = {"Jack", "Bob", "Alice"};
And I need to convert it as int array of length of elements
int[] lengths = {4, 3, 5};
So far I tried as following but is there any built-in method or better solution?
public class ArrayLengthTest{
public static void main(String []args){
String[] names = {"Jack", "Bob", "Alice"};
int[] lengths = new int[names.length];
for (int i = 0; i < names.length; i++) {
lengths[i] = names[i].length();
}
System.out.println(java.util.Arrays.toString(lengths));// print [4, 3, 5]
method1(lengths);// passing length array
method2(lengths);// passing length array
method3(lengths);// passing length array
}
}