Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MetaKnight

[java] Does Java have pointers?

This topic is 5733 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

Advertisement

  
public class ListNode
{
public ListNode Next;
public object Data;
}

It''s not a very good linked list, but you get the picture.



"If there is a God, he is a malign thug."
-- Mark Twain

Share this post


Link to post
Share on other sites
But what if i want to make a linked list for a grid of squares
like emagine a grid of squares going from 1 - 10 up
and A to D right
i want
object a1 to be linked to a2
and b1 to be linked to a1

a1 has a2 as a class
but a2 cant change a1


Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines


    
public class ListNode
{
public ListNode Next;
public object Data;
}

It''s not a very good linked list, but you get the picture.



"If there is a God, he is a malign thug."
-- Mark Twain


Wouldn''t that take an infinite amount of memory, because you''d have listnode.next.next.next.next.etc until you ran out of memory? Maybe I''m just being stupid...

-~-The Cow of Darkness-~-

Share this post


Link to post
Share on other sites
quote:
Original post by cowsarenotevil

Wouldn''t that take an infinite amount of memory, because you''d have listnode.next.next.next.next.etc until you ran out of memory? Maybe I''m just being stupid...

-~-The Cow of Darkness-~-


You''d have to set it up that way. Normally you could just "make" an iterator for this type by using:

// n is a reference to the first object in a list of ListNode objects. 

ListNode tmp=n;

while(tmp!=null)
{
// Do something with tmp here.

// Get the next object.
tmp = tmp.Next;
}


But if you had:
ListNode n = new ListNode();
n.Next = n;


before your while-loop "iterator", you WOULD run into that problem. But if you''re not an idiot then this shouldn''t become a problem.

Share this post


Link to post
Share on other sites
In Java, doing a ClassName Var; doesn''t allocate memory for it like in c++. You gotta use ClassName Var = new ClassName();

So no, it wouldn''t make an infinite loop

Share this post


Link to post
Share on other sites
quote:
Original post by cowsarenotevil
Wouldn''t that take an infinite amount of memory, because you''d have listnode.next.next.next.next.etc until you ran out of memory? Maybe I''m just being stupid...


ListNode Next in Java is semantically pretty much the same as ListNode* Next in C++.





"If there is a God, he is a malign thug."
-- Mark Twain

Share this post


Link to post
Share on other sites
quote:
Original post by MetaKnight
But what if i want to make a linked list for a grid of squares
like emagine a grid of squares going from 1 - 10 up
and A to D right
i want
object a1 to be linked to a2
and b1 to be linked to a1

a1 has a2 as a class
but a2 cant change a1


I didn''t really understand that. Could you please try to rephrase your question?





"If there is a God, he is a malign thug."
-- Mark Twain

Share this post


Link to post
Share on other sites
for exmaple if i was to use your code

public class ListNode{
public ListNodechild Next;
public object Data;
}

listnodechild couldnt effect listnode
how could i do that since i dont have pointers?
cause with pointers i could make them point to each other


Share this post


Link to post
Share on other sites
As someone already said, when you say ListNode somenode; in Java, you are not creating an object, as you would be in C++, but merely a reference to an object - a reference being the closest Java comes to a (C/C++)-style pointer. In order to instantiate it, you must use new (thus all objects in Java are heap allocated). Therefore, you can use a reference much as a pointer; two objects can refer to each other.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!