Saturday, 15 August 2015

java - Type safety: The expression of type ArrayList[] needs unchecked conversion -



java - Type safety: The expression of type ArrayList[] needs unchecked conversion -

i working on project in getting typesafety issues on initializing 1 method particular size. in run method, have yellowish line on new arraylist[tablelists.size()] , complaining about-

type safety: look of type arraylist[] needs unchecked conversion conform arraylist<method>[]

below code.

private arraylist<method> methods[] = null; @override public void run() { methods = new arraylist[tablelists.size()]; }

how can prepare typesafety issue here?

updated:-

int j = 0; dbconnection = new connection[tablelists.size()]; callablestatement = new callablestatement[tablelists.size()]; methods = new arraylist[tablelists.size()]; //loop around map values , create connection list (map<string, string> map : tablelists.values()) { dbconnection[j] = getdbconnection(map.get("url"), map.get("user"), map.get("password"), map.get("driver")); callablestatement[j] = dbconnection[j].preparecall(map.get("sql")); methods[j] = getrequiredmethods(map.get("suffix")); j++; }

since methods don't seem separated in particular way, why not store them in same arraylist, bypassing problem entirely?

arraylist<method> methods; /* ... */ methods.addall(getrequiredmethods(map.get("suffix")));

arrays , generics tend not play nice each other. if really need separation, have primary options:

use arraylist<arraylist<method>> instead of array. allow handle nicely without mucking plain arrays. can efficiently initialize methods object size regular array.

methods = new arraylist<arraylist<method>>(tablelists.size()); if really need utilize array, you'll have suppress warning using @suppresswarnings("unchecked") annotation. ugly , annoying, i'd avoid if can.

java multithreading casting

No comments:

Post a Comment