简体   繁体   English

如何在Java中将字符串数组转换为长度数组

[英]How to convert String array to length array in java

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. 我正在使用Java 1.7 ,需要将String数组转换为int数组,其中包含String数组中每个元素的长度。

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. 提醒:我需要多次将长度数组作为方法的参数传递,由于某些机密问题,我希望传递String数组。 Thats why I need to store lengths as int array. 那就是为什么我需要将长度存储为int数组。

For example, I have a String array something as 例如,我有一个String数组,例如

String[] names = {"Jack", "Bob", "Alice"};

And I need to convert it as int array of length of elements 我需要将其转换为元素长度的int数组

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
     }
}

JDK版本为1.8或更高版本时,请尝试此操作

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

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(); 使用名称[i] .length(); in iteration. 在迭代中。

If you are using JDK 1.8 then answer of saka will help. 如果您使用的是JDK 1.8,那么对saka的回答将有所帮助。

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. 但是正如您所说的,您使用的是Java 1.7,您只能选择手动遍历并手动创建数组。

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
 }
}

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. 但是,如果要动态构建String数组,那么在将每个String分配给String数组时,也可以构建lengths数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM