Skip to content
Advertisement

How to filter an ArrayList that has multi members in each element?

I have a function that reads air pollution data from a file and save it to an Array List. I’d like to filter the elements and print out the result.

I’m able to get a row from the ArrayList with System.out.println("1st element " + pollution.get(0)) but don’t know how to apply a filter to a single column.

The data file looks like this

JavaScript

The AirPollution class

JavaScript

Function that read the file and save it to an Array List

JavaScript

Print out all the records in the ArrayList that have a PM2.5 concentration 300 and over.

** This is the function I’d like to make changes to.**

JavaScript

How to achieve the result with minimal changes to the current code ? Thanks

Advertisement

Answer

It isn’t clear from your class fields what PM2.5 is but you can print each entry that has a pm >= 300 like this. Since your class fields are private it presumes you have getters to obtain the fields.

JavaScript

Note: Your toString() implementation does not have pm included in the returned string.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement