5 Replies - 389 Views - Last Post: 02 December 2011 - 12:23 AM Rate Topic: -----

#1 Xelorz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 11

Objects being added to a linked list by reference instead of value?

Posted 01 December 2011 - 08:05 PM

So I'm trying to learn java by building a game. I know a great deal of other languages. I want to create a Linked List of Point objects. So I created a linked list as such:

private LinkedList <Point>theSnake;
theSnake = new LinkedList<Point>();



Now I want to add 3 points to the link list and I've done this by creating a temporary variable and using setLocation to change the location of the point the adding it to the Link List as such:

Point temp = new Point();

temp.setLocation(center,center+1);
theSnake.add(temp);

theBoard[center][center+1] = snake;

        
temp.setLocation(center,center);
theSnake.add(temp);
theBoard[center][center] = snake;

        
temp.setLocation(center,center-1);
theSnake.add(temp);



theBoard is just an integer array and snake is just a static variable for a number and center is just some value. Anyways. When I print out the Linked List right after this snippet of code, the Link List is completely filled with the last value of temp. Just literally (center,center-1) three times. Which means that "temp" is being added to the linked list by reference. I'm so confused. I thought java never did this and everything was done by value. Eitherway is there a way to only add the value of temp to the linked list instead of its reference?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Objects being added to a linked list by reference instead of value?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Objects being added to a linked list by reference instead of value?

Posted 01 December 2011 - 08:09 PM

False problem just based on your topic title
You simply can't have an object "by value" in Java
In Java all objects are just pointers to an object

Point temp = new Point();

temp.setLocation(center,center+1);
theSnake.add(temp);

theBoard[center][center+1] = snake;

        
temp.setLocation(center,center);
theSnake.add(temp);
theBoard[center][center] = snake;

        
temp.setLocation(center,center-1);
theSnake.add(temp);



the 3 temp pushed into theSnake are the same they are actually in C/C++ world &temp

So you push in Snake three times &temp... 3 times the same pointer

changing one of the property of the object point to does change the same object the others point to
*temp.x = xxxx

A Java Vector is an array of 4 bytes addresses (pointers to the object)
A Java LinkedList has 4 bytes pointers to the next *Point

So you will add to add(new Pointer(x,y)) each time

P.S.
don't worry about keeping a pointer to those Objects, Java will free them for you if they are not used anymore :)

This post has been edited by pbl: 01 December 2011 - 09:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 Xelorz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 11

Re: Objects being added to a linked list by reference instead of value?

Posted 01 December 2011 - 10:22 PM

So is there a class or anything build in that I could use similar to a linked list that uses primitive data types like int instead so that it will pass by value? The only other way I know how achieve my goal is by using some array that I would set to some incredibly large number.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Objects being added to a linked list by reference instead of value?

Posted 01 December 2011 - 10:44 PM

Array of incredible size will take less memory than a LinkedList that also require a forward pointer
Yes autoboxing will convert your int into an Integer object but you should not worry about it

This post has been edited by pbl: 01 December 2011 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Objects being added to a linked list by reference instead of value?

Posted 01 December 2011 - 10:54 PM

You will have the same problem in C++
So what is your problem ?
We are far from you original pseudo problem :)

This post has been edited by pbl: 01 December 2011 - 10:57 PM

Was This Post Helpful? 0
  • +
  • -

#6 Xelorz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 11

Re: Objects being added to a linked list by reference instead of value?

Posted 02 December 2011 - 12:23 AM

Thank you much. I just went with the massive array. I was just kind of hoping java would have some magic solution. It seems to have a class built to solve almost everything :-P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1