Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multi-Dimensional Generic Arrays  (Read 1703 times)
0 Members and 1 Guest are viewing this topic.
Offline UprightPath
« Posted 2012-01-29 01:56:50 »

This here's a conundrum I've been trying to slog through for some time now. Part of it is a decided dislike of List type objects and part of it is issues with the way Generics work in Java. Here's the problem:

The game I'm designing is going to use an map using X dimensions (Where X >= 1). This base map is most easily described as a dense rectangular array (For X == 2), or a dense cubic array (For X == 3), etc. On "Top" of this is a very sparse map, with the same dimensions of the previous map, and on top of that can be another sparse map, and on top of that can be another, ad nauseum.

Here's a cobbled together example:
Layer 1 (Dense): Physical description of world
Layer 2 (Sparse): Location of hidden items
Layer 3 (Sparse): Location of special ground effects (Think this area is on fire/occupied by poison gas)
Layer 4 (Sparse): Location of Entities

For the most part, the Sparse maps are fairly simple to use Generics for. A HashMap<Position, T> takes care of it. For the Dense map on the bottom, it becomes more difficult to use Generics for, since
1  
T[][] map = new T[x][];
is a violation. However, using ArrayList<ArrayList<T>> leads to problems with replacement/excess size issues. HashMap<Position, T> may work, but again, since there's going to be a static number of values in the map it seems like a waste.

Are there already any 'good', for a given value of good or reason, replacement for dense two-dimensional staticly sized arrays?

Offline Danny02
« Reply #1 - Posted 2012-01-29 18:45:52 »

why do you want to use a generic array anyway. You said that u wanted 1 dens map and then many sparse ones, so having a generic sparse map makes sense.
But why not just have dense map take a class or an interface if you need to
Offline Waterwolf

Junior Devvie


Medals: 3



« Reply #2 - Posted 2012-01-29 19:41:51 »

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Array.html#newInstance(java.lang.Class,%20int[])
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #3 - Posted 2012-01-30 02:04:00 »

Well, the reason for the generics is because I want to reuse the code that I'm writing for the dense map in other places without having to change it too drastically between uses.

Waterwolf's suggestion should work, better than my current choice (Which is to have an object array and control the insertion/retrieval directly through methods, a la Java's ArrayList).

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (37 views)
2014-12-09 22:41:13

BurntPizza (73 views)
2014-12-08 04:46:31

JscottyBieshaar (34 views)
2014-12-05 12:39:02

SHC (47 views)
2014-12-03 16:27:13

CopyableCougar4 (42 views)
2014-11-29 21:32:03

toopeicgaming1999 (110 views)
2014-11-26 15:22:04

toopeicgaming1999 (96 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!