Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (813)
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 1080 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 Wizard


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


JGO's Spiffy Duchess


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

Welcome to a new kind of tension
All across the alienation
Where everything isn't meant to be okay
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: 485
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: 485
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.

 
cybrmynd (137 views)
2017-08-02 12:28:51

cybrmynd (157 views)
2017-08-02 12:19:43

cybrmynd (151 views)
2017-08-02 12:18:09

Sralse (167 views)
2017-07-25 17:13:48

Archive (640 views)
2017-04-27 17:45:51

buddyBro (764 views)
2017-04-05 03:38:00

CopyableCougar4 (1297 views)
2017-03-24 15:39:42

theagentd (1262 views)
2017-03-24 15:32:08

Rule (1235 views)
2017-03-19 12:43:22

Rule (1310 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!