简体   繁体   中英

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. What code could I use so that I can call, for example, examp1.getCount and examp2.getCount, and get the two different sizes?

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;

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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