java - Avoid unchecked warning in following -
I have done some fancy wrapping to avoid uncontrolled warnings in the past, but after over 90 minutes, Write down the findMatch method below and work it without @SuppressWarnings ("uncheck"). Parametized class is not known at compile time.
Public interfaces matchable & lt; T & gt; {Public boolean matches (T-tomech); } Public square placemarching {public static object search match (object-to-match, object [] to search) (if (for example (! For matches)) blank return; Matchable match oz = (matchable) & Lt ;? & gt; matchclass = match obj.get class (); (object obj: toSearch) {/ ** * Check here that the search list is for us to test * about object, such as match object Is the same category. * This means that the mill Non-qualified class will work without exception. ** / (matchClass.isInstance (obj) & matchObj.matches (obj) back obj;} // It did not return the tap;}}
The code works because in each case it is implemented by the matching t.
Apple tools matchable ORANGE OPTION MANAGEMENT & Edit
Edit: Add some test code
public static zero main (string [] args) {Object T [] randomList = createAppleArray (); Object apple = new apple ("red"); object match = search match (apple, random list); } Private stationary object [] Make Applar () {Return to new object] [New apple ("Pink"), New Apple ("Red"), New Apple ("Green")}; } Public-grade Apple tools matchable & lt; Apple & gt; {String color; Public apple (string color) {this.color = color; } Public Boolean matches (Apple apple) {return color.equals (apple.color); }}
public static & lt; T matchable & lt; T & gt; & Gt; T search match (t tomech, t [] to search) {if (toMatch == blank) return tap; Comparable & lt; T & gt; Matchab = tomech; Class & lt ;? & Gt; Matchclass = match oz .gate class (); For (T obj: toSearch) {if (matchClass.isInstance (obj) and matchObj.matches (obj) obj; } Return tap; }
Comments
Post a Comment