Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (686)
Games in Android Showcase (198)
games submitted by our members
Games in WIP (758)
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 684 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: 84
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();}
   }

hey missy your so fine you blow my mind hey missy

blog | buy tetradecagon on steam!
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.

 
roseslayer (545 views)
2016-08-06 11:43:29

roseslayer (500 views)
2016-08-06 09:43:11

xTheGamerCodes (574 views)
2016-08-04 15:40:59

xTheGamerCodes (569 views)
2016-08-04 15:40:24

orrenravid (914 views)
2016-07-16 03:57:23

theagentd (991 views)
2016-07-11 14:28:54

Hydroque (1089 views)
2016-07-06 05:56:57

Hydroque (1057 views)
2016-07-03 08:52:54

GrandCastle (873 views)
2016-07-01 09:13:47

GrandCastle (645 views)
2016-07-01 09:09:45
Rendering resources
by Roquen
2016-08-08 05:55:21

Rendering resources
by Roquen
2016-08-08 05:52:42

Rendering resources
by Roquen
2016-08-08 05:50:38

Rendering resources
by Roquen
2016-08-08 05:49:53

Rendering resources
by Roquen
2016-08-08 05:32:39

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
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!