Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  returning a new object from an array  (Read 322 times)
0 Members and 1 Guest are viewing this topic.
Offline lcass
« Posted 2014-04-20 20:21:40 »

so I am wanting to return a new object of the object type that is in an array. Is it possible, for example of waht I want to do   objecta[] as = new objecta[1]; return new objecta[1]
Offline SwordsMiner

JGO Coder


Medals: 6
Projects: 2
Exp: 1 year


The one and only.


« Reply #1 - Posted 2014-04-20 20:28:21 »

Yes. You would do something along of the lines of:

1  
2  
3  
4  
5  
private Object returnNewObject(int index){
      Object obj = YOURARRAY[index].getConstructor(/*This is where the classes of the objects the thing your creating go*/).newInstance(/*The instances of the objects the thing your creating goes*/);
      //modify here if nessesary
      return obj;
   }


Basically what its doing is asking the class to instansiate a copy of itself and then it returns the copy. Heres an example from my game you can reference to.

1  
2  
3  
4  
5  
6  
7  
protected void shoot(){
      try{
         Projectile p = projectile.getClass().getConstructor(Game.class).newInstance(game).addImmuneEntity(this);
         p.setPosition(x, y).setTrajectory(targetEntity.x*(int)side, targetEntity.y);
         game.registerEntity(p);
      } catch(Exception e){e.printStackTrace();}
   }

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline lcass
« Reply #2 - Posted 2014-04-20 21:25:45 »

what if im attempting to do it to an abstract ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #3 - Posted 2014-04-20 21:30:26 »

I can't tell exactly what you mean, is it something like this? http://stackoverflow.com/questions/529085/how-to-generic-array-creation
Offline lcass
« Reply #4 - Posted 2014-04-20 21:38:04 »

I have a super Tile() which has subclasses such as space and hull etc the Tiles[] arraystores the subclasses as objects, how would I return a new one.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #5 - Posted 2014-04-20 21:46:10 »

Why not have a
Class<? extends Tile>[]
? Then you can just tiles[3].newInstance();

There's probably a better solution, but to keep your array format, this is what I would do.
Offline Danny02
« Reply #6 - Posted 2014-04-21 08:40:02 »

Pls don't use reflection in this way.
One better solution would be to use factories. Factories are Objects/Classes which know how to instantiate a specific Object.

I don't know why you want to store these factories in an array, but you could use Enums for example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
interface Factory<E>
{
  E create();
}

enum TileFactories implements Factory<Tile>
{
   GRASS {
      Tile create(){ return new GrassTile(); }
   },
   ROCK {
      Tile create(){ return new RockTile(); }
   },
   SPECIAL_ROCK {
      Tile create(){
        RockTile t  = new RockTile();
        t.setSpezial(true);
        return t;
      }
   }
}


Tile t = TileFactories.getValues()[1].create();
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.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
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!