Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  Generics, where it all began..  (Read 3235 times)
0 Members and 1 Guest are viewing this topic.
Haksu
Guest
« Posted 2008-01-02 02:03:39 »

Hello,

I started my coding hobby year ago wit jdk 6. Generics have been supported since jdk 5 so I never had bad problems using and adding multiple objects, for example, in shooting games because I was able to use ArrayLists and  LinkedLists when I wanted to add multiple shots (like shooting with machine gun).

(My question comes after this example:)

For example I painted all the machine gun bullet objects just with this simple code:

for (Bullet b : bullet){
    b.paint(g);
}

and in Thread there was something like this:

for( Bullet b : bullet){
    b.move();
}

and I added new bullets in Keypressed like this:

bullet.add(new Bullet(some,parameters);


Now my question is that how things were done before the generics? I am doing one mobile shooting game and I cannot use generics in that. Creating many bullet objects without collections would be very time consuming.

I hope some on can help me with this.

Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2008-01-02 02:46:26 »

You can still use collections, you just have to manually cast things that you get out of them, like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
List bullets = new LinkedList();

// bang!
bullets.add( new Bullet() );

for( Object o : bullets )
{
   ( ( Bullet ) o ).move();
}


The downside, apart from the extra tedious typing, is that the nice compile-time error you get when you try to add a non-bullet object to the bullet list becomes an execution-time ClassCastException when you retrieve and cast the erroneous object.
Haksu
Guest
« Reply #2 - Posted 2008-01-02 04:14:50 »

You can still use collections, you just have to manually cast things that you get out of them, like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
List bullets = new LinkedList();

// bang!
bullets.add( new Bullet() );

for( Object o : bullets )
{
   ( ( Bullet ) o ).move();
}


The downside, apart from the extra tedious typing, is that the nice compile-time error you get when you try to add a non-bullet object to the bullet list becomes an execution-time ClassCastException when you retrieve and cast the erroneous object.

Thanks!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2008-01-02 19:37:11 »

Generics simply add the proper class-casts when compiling.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Haksu
Guest
« Reply #4 - Posted 2008-01-03 16:48:05 »

You can still use collections, you just have to manually cast things that you get out of them, like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
List bullets = new LinkedList();

// bang!
bullets.add( new Bullet() );

for( Object o : bullets )
{
   ( ( Bullet ) o ).move();
}


The downside, apart from the extra tedious typing, is that the nice compile-time error you get when you try to add a non-bullet object to the bullet list becomes an execution-time ClassCastException when you retrieve and cast the erroneous object.

Oops, it didn't work.

I should have put more emphasize on that I'm doing midp application.  I'm doing cldc 1.0 and midp 1.0 application, so I don't have linkedLists and I cannot use these for-each loops which became same time with generics, I think.

Only collections that I can use are Vector, Stack, Hashtable and Enumeration.

So how can I do it with these resources?
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #5 - Posted 2008-01-03 17:13:49 »

You can use a Vector like you would a LinkedList, and an Enumeration like you would an Iterator, like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Vector bullets = new Vector();

// bang!
bullets.add( new Bullet() );

Enumeration b = bullets.elements();
while( b.hasMoreElements() )
{
   ( ( Bullet ) b.nextElement() ).move();
}


but, to pre-empt Riven's forthcoming reply, a Vector has O(1) access to it's elements (Unlike a LinkedList), so it's better to bypass the Enumeration bit and access directly:

1  
2  
3  
4  
for( int i = 0; i < bullets.size(); i++ )
{
   ( ( Bullet ) bullets.get( i ) ).move();
}



...especially in JME environments where the garbage collector probably isn't as clever as you'd like.
Haksu
Guest
« Reply #6 - Posted 2008-01-03 17:46:48 »

Thanks, I'll try it.
Haksu
Guest
« Reply #7 - Posted 2008-01-05 02:52:49 »

You can use a Vector like you would a LinkedList, and an Enumeration like you would an Iterator, like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Vector bullets = new Vector();

// bang!
bullets.add( new Bullet() );

Enumeration b = bullets.elements();
while( b.hasMoreElements() )
{
   ( ( Bullet ) b.nextElement() ).move();
}


but, to pre-empt Riven's forthcoming reply, a Vector has O(1) access to it's elements (Unlike a LinkedList), so it's better to bypass the Enumeration bit and access directly:

1  
2  
3  
4  
for( int i = 0; i < bullets.size(); i++ )
{
   ( ( Bullet ) bullets.get( i ) ).move();
}



...especially in JME environments where the garbage collector probably isn't as clever as you'd like.


Thank you very much! Even if I'm drunk now, I got it work!

Are you sure that for- thing works like this:
1  
2  
3  
4  
for( int i = 0; i < bullets.size(); i++ )
{
   ( ( Bullet ) bullets.get( i ) ).move();
}

Because there doesn't seem to be "get" - method..

Program is working as well as I hoped so Thank's again!


Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #8 - Posted 2008-01-05 11:22:35 »


Because there doesn't seem to be "get" - method..


You're right there, I was looking at the j2se Vector javadocs. In CDLC, it's .elementAt( int index )
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.

atombrot (25 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!