Does an object returned from a method return a non-const reference?

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).


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…


/**
 *
 * @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!

  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 :frowning:

Thanks dude!!!

I now understand why the code works.

Note: C++ templates and java generics have nothing in common. Templates are a type of macro and generics are a type of contract.