数组Java中每个元素的长度 [英] length of each element in an array Java
本文介绍了数组Java中每个元素的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我正在尝试使用的内容。方法.length对我尝试的任何东西都不起作用,所以我甚至不知道从哪里开始。
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 class LengthsOfStrings {
public static ArrayList<Integer> lengths(ArrayList<String> list) {
ArrayList<Integer> lengthList = new ArrayList<Integer>();
for (int nums: lengthList) {
System.out.println(nums.length());
}
return lengthList;
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("Ciao");
list.add("Moi");
list.add("Benvenuto!");
list.add("badger badger badger badger");
ArrayList<Integer> lengths = lengths(list);
System.out.println("The lengths of the Strings: " + lengths);
}
}
推荐答案
你正在尝试迭代单个 int
而不是字符串数组。更改
You are trying to iterate through a single int
instead of the strings array. Change
for (int nums: lengthList) {
System.out.println(nums.length());
}
到
for (String str: list) { // loop through the list of strings
lengthList.add(str.length()); // store the individual length of each string
}
以便遍历列表字符串,收集每个字符串&的长度
将它存储在 Arraylist
中,以后返回
。
so as to loop through the list of strings, collect the length
of each string & store it int the Arraylist
that you return
later.
这篇关于数组Java中每个元素的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文