Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 585 times)
0 Members and 1 Guest are viewing this topic.
Offline relminator
« Posted 2013-03-07 05: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: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2013-03-07 05: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
« Reply #2 - Posted 2013-03-07 10: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!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2013-03-07 11: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  
 
 
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.

Dwinin (21 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!