Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compare the size of two arrays and return a larger array(not value or size)?

I’m trying to write a method that will return the larger of the two arrays.

List<String> firstName = new ArrayList<String>(); // size 50
List<String> lastName = new ArrayList<String>(); // size 1000

I want to compare their size and return a larger array. Something like this.

public List<String> largerArray (List firstName, List lastName) {
return max.size(firstName, lastName);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

public List<String> largerArray (List firstName, List lastName) {
    return firstName.size() > lastName.size() ? firstName : lastName;
}

Although your use case is not very clear. If firstName is a list then ideally it must be named as firstNames, if its not the case then i am not sure what exactly you’re trying to do there

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading