Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  Does an object returned from a method return a non-const reference?  (Read 304 times)
0 Members and 1 Guest are viewing this topic.
Offline relminator

Senior Member


Medals: 2
Projects: 1



« Posted 2013-03-07 06:19:58 »

Hello, (java noob warning)

I was reading about generics(works like templates in c++) and decided to write a generic LinkedArray Class (I plan to use this not only for bullets but particles and enemies).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
import java.util.ArrayList;

/**
 *
 */


/**
 * @author Anya
 *
 */

public class LinkedArray<T>
{

   private ArrayList<T> elements = new ArrayList<T>();
   
   private int size;
   
   private int freeElements = 0;
   private int activeElements = 0;
   
   private int[] freeElementIndex;  
   
   public LinkedArray( int size, T e )
   {
      this.size = size;
      this.activeElements = 0;
      this.freeElements = size;
      this.freeElementIndex = new int[size];
     
      elements.ensureCapacity(size);
     
      for( int i = 0; i < size; i++ )
      {
         elements.add(e);
      }
     
      for( int i = 0; i < size; i++ )
      {
         freeElementIndex[i] = i;
      }
     
   }
   
   public int getSize()
   {
      return size;
   }
   
   public int getActiveElements()
   {
      return activeElements;
   }
   
   public void add( T e )
   {
     
      if( freeElements == 0 )
      {
         return;
      }
     
      freeElements--;
     
      int index = freeElementIndex[freeElements];
      elements.set( index, e );
     
     
      activeElements++;
     
   }
   
   void remove( int index )
   {
     
      freeElementIndex[freeElements] = index;
         freeElements++;
      activeElements--;
         
   }

   public T get( int index )
   {
      return elements.get( index );
   }
   
}


And here's how it is used...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
/**
 *
 * @author Richard Eric M. Lope (Relminator)
 * @version 1.00 2013/3/04
 */


import java.awt.Graphics2D;


public class BulletFactory
{
   
   private LinkedArray<Bullet> bullets;
   
   public BulletFactory( int size )
   {
     
      Bullet b = new Bullet();
      bullets = new LinkedArray<Bullet>( size, b );
         
   }
   
   public int getSize()
   {
      return bullets.getSize();
   }
   
   public int getActiveBullets()
   {
      return bullets.getActiveElements();
   }
   
   public void addBullet( Bullet bullet )
   {
     
      bullets.add( bullet );
   }
   
   void removeBullet( int index )
   {
      bullets.remove( index );
   }
   
   public void updateBullets()
   {
      for( int i = 0; i < bullets.getSize(); i++ )
      {
         Bullet b = bullets.get(i);
         
         if( bullets.get(i).isActive() )
         {
            b.update();
            if( (b.position.x < 0) || (b.position.x > Globals.SCREEN_WIDTH) ||
               (b.position.y < 0) || (b.position.y > Globals.SCREEN_HEIGHT) )
            {
               b.kill() ;
               bullets.remove(i);
            }
         }
      }
     
   }
   
   public void renderBullets( Screen screen, Graphics2D g, ImageAtlas imageAtlas )
   {
     
      for( int i = 0; i < bullets.getSize(); i++ )
      {
         Bullet b = bullets.get(i);
         
         if( b.isActive() )
         {
            int frame = b.getFrame();
            g.drawImage( imageAtlas.getSprite(frame),
                      (int)b.position.x - imageAtlas.getSprite(frame).getWidth()/2,
                      (int)b.position.y - imageAtlas.getSprite(frame).getHeight()/2,
                      screen );
         }
      }
     
   }
   
}


My questions are:

1.  Why does updateBullets() and renderBullets() work?  Since I made a local copy of the bullet b from bullets.get(index).  Does Bullet b and bullets.get(i) share the same reference and is "deep-copied"?
*Note that I did that code out of curiosity and crossed my fingers it works.

2. Why can't I use normal arrays (vs arrayList ) when doing generic classes?


Thanks!
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #1 - Posted 2013-03-07 06:42:32 »

1. You are not making a copy of the object itself, only the pointer. 'b' points to the object stored inside the ArrayList at index 'i'. The "by value" part of Java means that you are *not* modifying the original pointer, you are getting a copy of the pointer.

2. Java currently does not allow generic arrays Sad

Offline relminator

Senior Member


Medals: 2
Projects: 1



« Reply #2 - Posted 2013-03-07 11:31:10 »

1. You are not making a copy of the object itself, only the pointer. 'b' points to the object stored inside the ArrayList at index 'i'. The "by value" part of Java means that you are *not* modifying the original pointer, you are getting a copy of the pointer.

2. Java currently does not allow generic arrays Sad

Thanks dude!!!  

I now understand why the code works.

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Roquen

JGO Ninja


Medals: 66



« Reply #3 - Posted 2013-03-07 12:37:29 »

Note: C++ templates and java generics have nothing in common.  Templates are a type of macro and generics are a type of contract.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (37 views)
2013-05-17 21:29:12

alaslipknot (46 views)
2013-05-16 21:24:48

gouessej (75 views)
2013-05-16 00:53:38

gouessej (75 views)
2013-05-16 00:17:58

theagentd (83 views)
2013-05-15 15:01:13

theagentd (77 views)
2013-05-15 15:00:54

StreetDoggy (119 views)
2013-05-14 15:56:26

kutucuk (143 views)
2013-05-12 17:10:36

kutucuk (143 views)
2013-05-12 15:36:09

UnluckyDevil (153 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.144 seconds with 21 queries.