length of each element in an array Java

后端 未结 5 541
不思量自难忘°
不思量自难忘° 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:46

    You are trying to iterate through a single int instead of the strings array. Change

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

    to

    for (String str: list) { // loop through the list of strings
        lengthList.add(str.length()); // store the individual length of each string
    }
    

    so as to loop through the list of strings, collect the length of each string & store it int the Arraylist that you return later.

提交回复
热议问题