How do you properly nest multiple ArrayLists / Maps in Java? -


i trying run simple program, , i'm stuck on basics of declaring nested lists , maps.

i'm working on project requires me store polynomials arraylist. each polynomial named, want key/value map pull name of polynomial (1, 2, 3 etc.) key, , actual polynomial value.

now actual polynomial requires key values because nature of program requires exponent associated coefficient.

so example need arraylist of polynomials, first 1 simple:

polynomial 1: 2x^3

the array list contains whole thing map, , map contains key: polynomial 1 , value: map... 2 , 3 being key/values.

the code have below i'm not 100% on how format such nested logic.

public static void main(string[] args) throws ioexception{         arraylist<map> polynomialarray = new arraylist<map>();         map<string, map<integer, integer>> polynomialindex = new map<string, map<integer, integer>>();         string filename = "polynomials.txt";         scanner file = new scanner(new file(filename));          for(int = 0; file.hasnextline(); i++){             //this scan polynomials out of file , stuff          } 

edit: updated key/value in map, still having issues.

the code above giving me following error:

cannot instantiate type map<string,map<integer,integer>> 

so how go doing or going wrong way?

you can't instantiate new map<string, map<integer, integer>>() because java.util.map interface (it doesn't have constructor). need use concrete type java.util.hashmap:

map<string, map<integer, integer>> polynomialindex = new hashmap<string, map<integer, integer>>(); 

also, if you're using java 7 or above, can use generic type inference save typing:

map<string, map<integer, integer>> polynomialindex = new hashmap<>(); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -