POV-Ray : Newsgroups : povray.off-topic : A question about Java generics (not a flame) : Re: A question about Java generics (not a flame) Server Time
7 Sep 2024 13:26:27 EDT (-0400)
  Re: A question about Java generics (not a flame)  
From: Nicolas Alvarez
Date: 9 May 2008 13:17:40
Message: <48248734@news.povray.org>
Warp wrote:
>   Is there a way in Java to shorten those long type names?
> 

Not that I know of. In fact, it's even more annoying in that you have to
type the whole type twice when creating an object.

Map<String, ArrayList<ArrayList<String>>> theMap =
   new HashMap<String, ArrayList<ArrayList<String>>>();
ArrayList<ArrayList<String>> someList = new ArrayList<ArrayList<String>>();

Of course, you could also use interfaces for the type and generic
parameters, only using a real class for the actual object you're creating,
making it a bit shorter:

Map<String, List<List<String>>> theMap =
   new HashMap<String, List<List<String>>>();
List<List<String>> someList = new ArrayList<List<String>>();

//and let's continue the example
List<String> deeperList = new ArrayList<String>();
List<String> otherDeeperList = new ArrayList<String>();
deeperList.add("one");
deeperList.add("two");
otherDeeperList.add("three");
otherDeeperList.add("four");
someList.add(deeperList);
someList.add(otherDeeperList);
theMap.put("foo", someList);

It's interesting to note that in scripting languages, all that could be a
one-liner. In Javascript:
var theMap = {"foo": [["one", "two"], ["three", "four"]]};

And even more interesting to note that C++0x will allow an initializer
syntax almost as short as that :)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.