Jump to content
  • Advertisement
Sign in to follow this  
hisDudeness

Java Link Lists???

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I am trying to "convert" my relatively simply C++ program into Java. By "convert" I mean, literally go through the code line by line, and re-phrase C++ code as Java. Don't ask me why, it is a long story. My C++ program uses a singly-linked list. Java doesn't support pointers. How in god's green earth am I supposed to implement a link list in Java without pointers. Please, anybody, send me a sign that there is hope.... -hisDudeness

Share this post


Link to post
Share on other sites
Advertisement
There is hope.
Any non-POD Java type is automatically passed and used by reference, which is the C/C++ equivalent of pointers.

So just pretend that your type which needs to be a pointer is a pointer, and it'll work fine. Trust me, I created a linked list in Java not too long ago and it worked fine.

Cheers,
Twilight Dragon

Share this post


Link to post
Share on other sites
java does support pointers only they call them different. E.g Object o=new Object(); o is a pointer to Object.

Also Java has list class so you might want to check that out.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html

Share this post


Link to post
Share on other sites
Quote:
Original post by hoho
Also Java has list class so you might want to check that out.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html


Just in case the OP gets confused util.List is an interface, if you are going to use a reference to util.List you need to instantiate & assign a concreate type that implements the util.List interface. There are a thew imps to choose from. If all you need is a bog standard linked-list imp then its simply:


import java.util.*;
//......
List<foo> l = new LinkedList<foo>();


And you don't have to use a reference to util.List if you don't need polymorphic behaviour. By the way that is in java 1.5.

Share this post


Link to post
Share on other sites
The java.util.List interface and the LinkedList implementation have been around since before 1.5...maybe you were referring to something else?

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
The java.util.List interface and the LinkedList implementation have been around since before 1.5...maybe you were referring to something else?


Only 1.5 > * has support for generics, all the java collections framework have been change to reflect generics. That is what my code example can only work hence i'm right but just didn't go into details.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!