Java-Gaming.org Hi !
Featured games (88)
games approved by the League of Dukes
Games in Showcase (681)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (744)
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 665 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 Brynn

JGO Ninja


Medals: 54
Projects: 3
Exp: 1 month or less


I want a perfect body. I want a perfect soul.


« 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();}
   }

I don't care if it hurts. I want to have control. I want a perfect body. I want a perfect soul.

blog twitter steam utube
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: 482
Exp: 7 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: 482
Exp: 7 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.

 
Hydroque (48 views)
2016-06-22 02:17:53

SwampChicken (49 views)
2016-06-20 13:22:57

SwampChicken (50 views)
2016-06-20 13:22:49

SwampChicken (42 views)
2016-06-20 13:22:26

Hydroque (85 views)
2016-06-15 08:22:50

Hydroque (77 views)
2016-06-13 06:40:55

DarkCart (196 views)
2016-05-29 02:30:33

Hydroque (165 views)
2016-05-26 14:45:46

Mac70 (169 views)
2016-05-24 21:16:33

theagentd (152 views)
2016-05-14 18:38:35
Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17
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!