java - Recompile my code with -Xlint:unchecked.After recompiling with -Xlint , unchecked call warning appears . how do i run my program now? -
/** traversing elements */ import java.util.*; class traversor extends thread { enumeration e; public traversor(enumeration e) { this.e=e; } public void run() { system.out.println("new thread started, traversing vector elements....."); while(e.hasmoreelements()) { system.out.println(e.nextelement()); try { thread.sleep(4000); } catch(exception ex) {} } system.out.println("new thread completed"); } } class vectortest{ public static void main(string args[]) { system.out.println("main thread started, creating vector..."); vector v=new vector(); v.addelement("one"); v.add("three"); v.add(1,"two"); enumeration e=v.elements(); system.out.println("vector created ,enumeration obtained"); traversor th=new traversor(e); th.start(); system.out.println(" new thread launched , suspending main thread"); try{ thread.sleep(1000); } catch(exception ex) {} system.out.println("main thread resumed,modifying vector"); v.add("four"); v.add("five"); system.out.println("vector modified, main thread completed"); } }
java introduced (rather late) typing of collection items vector
of object
s vector<t>
, t
type parameter given, vector<string>
. (it called generic typing.)
now compiler can "check" whether item types alright.
class traversor<t> extends thread { enumeration<t> e; public traversor(enumeration<t> e) { this.e = e; } public void run() { system.out.println("new thread started, traversing vector elements....."); while (e.hasmoreelements()) { system.out.println(e.nextelement()); try { thread.sleep(4000); } catch (exception ex) { } } system.out.println("new thread completed"); } } class vectortest { public static void main(string args[]) { system.out.println("main thread started, creating vector..."); vector<string> v = new vector<>(); v.addelement("one"); v.add("three"); v.add(1, "two"); enumeration<string> e = v.elements(); system.out.println("vector created ,enumeration obtained"); traversor<string> th = new traversor<>(e); th.start(); system.out.println(" new thread launched , suspending main thread"); try { thread.sleep(1000); } catch (exception ex) { } system.out.println("main thread resumed,modifying vector"); v.add("four"); v.add("five"); system.out.println("vector modified, main thread completed"); } }
by way vector , enumeration rather old. 1 seldom sees vector nowadays.
// alternative vector: list<string> list = new arraylist<>(); list.add("alpha"); (string s : list) { ... } // alternative enumeration: iterator<string> iterator = list.iterator(); while (iterator.hasnext()) { string s = iterator.next(); }
Comments
Post a Comment