如何使用 on 方法获取两个不同大小的数组列表的大小?

我是 Java 新手,我想使用一种方法 getCount 来获取两个不同大小的数组列表的大小。例如,我可以使用什么代码调用examp1.getCount 和examp2.getCount,并获得两种不同的大小?



慕码人2483693
浏览 93回答 3
3回答

qq_遁去的一_1

坦率地说,这是一个最基本的问题,谷歌本可以很容易地帮助你解决这个问题。如果您想设计自己的方法,这就是您的全部方法。public static int getCount(ArrayList A){         return A.size();     }

扬帆大鱼

在您的方法中,用于.size()获取两个 ArrayLists 的大小并将大小存储在数组中,然后存储在该return数组中。

蝴蝶刀刀

假设您创建了 2 个列表 li1 和 li2 然后:包输出;import java.util.ArrayList;import java.util.List;public class CalculateLength {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> li1= new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; li1.add(1);&nbsp; &nbsp; &nbsp; &nbsp; li1.add(2);&nbsp; &nbsp; &nbsp; &nbsp; getCount(li1);&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> li2= new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; li2.add(1);&nbsp; &nbsp; &nbsp; &nbsp; li2.add(2);&nbsp; &nbsp; &nbsp; &nbsp; li2.add(3);&nbsp; &nbsp; &nbsp; &nbsp; li2.add(4);&nbsp; &nbsp; &nbsp; &nbsp; getCount(li2);&nbsp; &nbsp; }&nbsp; &nbsp; public static int getCount(List list) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("LEngth = " + list.size());&nbsp; &nbsp; &nbsp; &nbsp; return list.size();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java