-1

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
     }
}
mmuzahid
  • 2,252
  • 23
  • 42
  • Why can't you just use `names[i].length()` whenever you want length of a `String`? – Atri Mar 09 '16 at 05:15
  • Because length array consume by other methods multiple times. – mmuzahid Mar 09 '16 at 05:17
  • **Define** *better*, you could extract your current logic to a method. You might use a different loop, or more meaningful variable names; as it is you use three (perfectly reasonable) lines of code to instantiate and initialize the `lengths` array. – Elliott Frisch Mar 09 '16 at 05:18
  • 1
    @Atri in case user want to do complex operations after iteration, it is required to store the length of string elements stored in an array. so I have provided answer. – Hemant Metalia Mar 09 '16 at 05:18
  • @HemantMetalia why is it **required** to store the length of string in an array? – Atri Mar 09 '16 at 05:20
  • @Atri I've edited my question to answer your comment – mmuzahid Mar 09 '16 at 05:21
  • 1
    @mmuzahid even if it is used multiple times, `names[i].length()` will work just fine. `String.length()` in java is O(1) operation, if that is your concern. See here http://stackoverflow.com/questions/254726/in-java-for-a-string-x-what-is-the-runtime-cost-of-s-length-is-it-o1-or-o – Atri Mar 09 '16 at 05:23
  • @Atri thanks for your suggestion, but I need an int array to pass it other methods. And do not want to pass string array. – mmuzahid Mar 09 '16 at 05:33
  • @HemantMetalia Thanks for your understanding correctly – mmuzahid Mar 09 '16 at 05:42
  • @mmuzahid please upvote my question as people have downvoted 3 times without going in details – Hemant Metalia Mar 09 '16 at 06:13
  • @HemantMetalia same case for my question downvotes. I've already upvoted your answer – mmuzahid Mar 09 '16 at 06:16
  • @mmuzahid please mark my answer as accepted if it helped you. Thanks – Hemant Metalia Mar 09 '16 at 06:54

3 Answers3

6

Try this when JDK version is 1.8 or later

int[] lengths = Stream.of(names).mapToInt(String::length).toArray();
1

This is not generally required, so there is no such inbuilt methods to create a new array containing length of each string values.

The logic you mentioned is correct. you can improve the logic in case required.

If you require to use the length sometimes (not repetitive) you can directly access the length of the array element. using names[i].length(); in iteration.

If you are using JDK 1.8 then answer of saka will help.

int[] lengths = Stream.of(names).mapToInt(String::length).toArray();

but as you said you are using java 1.7 you have only option to iterate through and create array manually.

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
 }
}
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
0

If you are declaring your string array manually as you have done above, then there is no other efficient way than what you already have, any inbuilt method you use will have the same complexity. But, if you are building the String array dynamically, then you can also build the lengths array as you assign each String to the String array.

Zaid Qureshi
  • 1,203
  • 1
  • 10
  • 15