简体   繁体   中英

How to find the minimum element of a matrix of objects in java?

I have a Matrix class that has the following attributes: number of Lines, number of columns and a matrix of Objects.

I have to find the minimum element of the matrix of Objects. How could I do that if I don't know what my Objects will be in JAVA?

You can use either the comparable or comparator functions to, effectively sort arrays of objects by a specific value. You will, depending override the compare to value and select an instance field to make comparisons with.

  1. Use reflection to find out of the objects implement comparable and are all the same type or same base type, and that base type implements Comparable.
  2. if not go thru all the fields, for every field that is a string or a number, compare with corresponding... if its an object go in to that and find strings and numbers. Can make a generic comparator. If a field is a collection then poke in to the type and process similarly. Not an easy task but doable. You could also choose not to go more than x level deep by keeping a current level int that you pass around your stack (if you get in to properties that are objects)

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