Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  quick ArrayList question  (Read 606 times)
0 Members and 1 Guest are viewing this topic.
Offline Soljaragz

Jr. Member
**

Posts: 55



« on: 2006-02-06 20:23:54 »

is it me or does the get() method return the address of an object instead of a copy of an object

 like for example

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
public class z 
{
   public static void main(String[] args)
   {
     
      ArrayList list = new ArrayList();
      list.add(new Bob());
     
      System.out.println("Before Change");
     
      System.out.println( ( (Bob)list.get(0) ).getX() );
     
      Bob testObject = (Bob)list.get(0);
      testObject.a();
     
      System.out.println("After Change");
      System.out.println( ( (Bob)list.get(0) ).getX() );
   }
}
class Bob
{
   private int x =0;
   public Bob()
   {
     
   }
   public void a()
   {
      x++;
   }
   public int getX()
   {
      return x;
   }
}


Considering this code, if testObject is only a copy of whats in get(0), then if i do anything to testObject, wouldn't the element in get(0) stay the same??? but when i execute the code, the element at get(0) becomes changed when I didn't use any set methods at all.

Another example with iterator.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
public class test
{
   ArrayList a = new ArrayList();
   public test()
   {

      a.add(new bob());
   }  
   public ArrayList getList()
   {
      return a;
   }
}
class Bobtester
{
   public static void main(String[] args)
   {
      test t = new test();
      ArrayList b = t.getList();
      Iterator bi = b.iterator();
      while(bi.hasNext())
      {
         bob c = (bob)bi.next();
      }
      System.out.println(b);
      System.out.println(t.getList());
   }
}
class bob
{
   int x=0;
   public void at()
   {
      x++;
   }
   public String toString()
   {
      return Integer.toString(x);
   }
}


Basically ArrayList b is suppose to be a copy of ArrayList a, but when I remove each element from ArrayList b, each element is also removed from ArrayList a..........................why is that???
Offline jbanes

JGO Neuromancer
****

Posts: 1178


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 on: 2006-02-06 20:34:09 »

Welcome to object oriented programming. Enjoy your stay.

Java doesn't have pointers, though it is "okay" (sort of) to think of them as such if it helps you understand. Java has references instead. The easiest way to think of a reference is like this:

- Imagine you have a box full of stuff. Light bulbs, toys, spare parts, tools, whatever. The box only changes when you add something into it.
- Imagine now that you label each item in the box with a number. That number tells you which object you're referring to. You can always write in all your record books that you need part XYZ for some job. There's only one part though. You have to add another one if you need more.
- Imagine that when you have no more use for a part, you rip the label off. Your wife (being very tidy and hating all the junk you keep around) checks your box every once in awhile to see if there's any stuff without a label. If she finds anything, she throws it out. (Men and their junk, eh?)

The labels are like references. They allow you to always keep track of the specific instance of the object. The objects in the box are just like your Java objects. Your wife in this scenario, is the garbage collector. And finally, the box is an object too, so it just happens to be an ArrayList.

Make sense? Smiley

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #2 on: 2006-02-06 20:41:16 »

lol thats funny cause my Comp Sci teacher is telling me that get() returns only a copy.

but i dont understand something

in my previous 2 examples, the original list gets changed

but when i did another test, this time with Strings, the original list did NOT get changed, why is that?
1  
2  
3  
4  
5  
ArrayList b = new ArrayList();
      b.add("hey");
      String tester = (String)b.get(0);
      tester = tester.substring(0,1);
      System.out.println((String)b.get(0));
Games published by our own members! Go get 'em!
Offline jbanes

JGO Neuromancer
****

Posts: 1178


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #3 on: 2006-02-06 21:30:00 »

lol thats funny cause my Comp Sci teacher is telling me that get() returns only a copy.

Either you misunderstood, or he's wrong. *shrug*


Quote
in my previous 2 examples, the original list gets changed

The list didn't change. Only the object inside the list. Remember that.

Quote
but when i did another test, this time with Strings, the original list did NOT get changed, why is that?

Read the JavaDocs for substring. It specifically states, "Returns a new string that is a substring of this string." So you now have two objects. One that is the original string, and one that is a single letter.

Consider that "tester" is just a reference. What happens when you reassign the reference?

P.S. Always remember that Java Strings are immutable. You can't change them. Any method called on the string will always return a new string or data about the string. See the JavaDocs for more info.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline swpalmer

JGO Kernel
*****

Posts: 3438
Medals: 4


Where's the Kaboom?


« Reply #4 on: 2006-02-06 21:41:07 »

lol thats funny cause my Comp Sci teacher is telling me that get() returns only a copy.

Copy of the reference maybe.  Making copies in Java is usually explicit.. you construct a new object yourself or call .clone().  Unless a library method is documented as making a copy, it probably doesn't and simple uses a reference to the original object.  Sometimes it is the same for objects that you pass in to methods.  e.g. be careful when doing someObject.setSomeProperty(myX); myX.myField = somethingElse, because you might alter the property held in someObject when you don't intend to.  One of the reasons that Java uses immutable strings and immutable wrapper classes for all the primative types is to prevent that sort of problem.

Offline Soljaragz

Jr. Member
**

Posts: 55



« Reply #5 on: 2006-02-06 21:55:53 »

ok thanks i kind of understand
Offline Jeff

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #6 on: 2006-02-07 15:46:17 »

Damn, JB. you just said everything I was going to Cool

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.108 seconds with 20 queries.