Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  quick ArrayList question  (Read 900 times)
0 Members and 1 Guest are viewing this topic.
Offline Soljaragz

Junior Member





« Posted 2006-02-07 02: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 Coder


Projects: 1


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


« Reply #1 - Posted 2006-02-07 02: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

Junior Member





« Reply #2 - Posted 2006-02-07 02: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


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


« Reply #3 - Posted 2006-02-07 03: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 Coder




Where's the Kaboom?


« Reply #4 - Posted 2006-02-07 03: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

Junior Member





« Reply #5 - Posted 2006-02-07 03:55:53 »

ok thanks i kind of understand
Offline Jeff

JGO Coder




Got any cats?


« Reply #6 - Posted 2006-02-07 21: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]
  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.

xsi3rr4x (54 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!