java - Best way to initialize three dimension List -
is there better way allows me initialize three-dimensional arraylist?
list<list<list<string>>> lista = new arraylist<list<list<string>>>(3); (int i=0;i<3;i++){ list<list<string>> lista2 = new arraylist<list<string>>(4); (int j=0;j<4;j++)lista2.add(new arraylist<string>(5)); lista.add(lista2); }
without external libraries or introducing new classes -- can use double brace initialization. bit cleaner having intermediate variables.
public list<list<list<string>>> lista = new arraylist<list<list<string>>>(){{ for(int i=0;i<3;i++) add(new arraylist<list<string>>(){{ for(int i=0;i<4;i++) add(new arraylist<>(5)); }}); }};
Comments
Post a Comment