length of each element in an array Java

后端 未结 5 487
不思量自难忘°
不思量自难忘° 2021-01-29 15:41

Here is what I\'m trying to use. The method .length doesn\'t work for anything I try, so I\'m not even sure where to begin.

import java.util.ArrayList;

public c         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-29 16:25

    In the following code:

    ArrayList lengthList = new ArrayList();
        for (int nums: lengthList) {
            System.out.println(nums.length());  
        }
    

    You are creating an ArrayList lengthList which is empty and trying to iterate over it.

    I think you want to iterate over list something like this:

    for (String s: list) {
        lengthList.add(s.length());
        System.out.println(s.length());  
    }
    

    and add length() of each String to lengthList in each iteration.

提交回复
热议问题