简体   繁体   English

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

[英]How do I use on method to get the sizes of two different-sized arrayslists?

I'm new to Java and I want to use one method, getCount, to get the sizes of two arraylists that are not the same size.我是 Java 新手,我想使用一种方法 getCount 来获取两个大小不同的数组列表的大小。 What code could I use so that I can call, for example, examp1.getCount and examp2.getCount, and get the two different sizes?我可以使用什么代码来调用,例如,examp1.getCount 和examp2.getCount,并获得两个不同的大小?

This is frankly as basic a question as it gets, and google could have helped you with this easily.坦率地说,这是一个最基本的问题,谷歌可以轻松地帮助你解决这个问题。 Here's your entire method if you want to design your own.如果您想设计自己的方法,这里是您的整个方法。

public static int getCount(ArrayList A){
        return A.size();
    }

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

Suppose you create 2 lists li1 and li2 then : package output;假设您创建 2 个列表 li1 和 li2 然后: package output;

import java.util.ArrayList;
import java.util.List;

public class CalculateLength {

    public static void main(String[] args) {
        List<Integer> li1= new ArrayList<>();
        li1.add(1);
        li1.add(2);
        getCount(li1);
        List<Integer> li2= new ArrayList<>();
        li2.add(1);
        li2.add(2);
        li2.add(3);
        li2.add(4);
        getCount(li2);
    }

    public static int getCount(List list) {
        System.out.println("LEngth = " + list.size());
        return list.size();
    }
}

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

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