Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 18th, 2003, 11:03 AM
Registered User
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default vector.add(); -- Reference or Object?

Hi all!

I need some help here! =) If i create one object of a class:

Player newPlayer = new Player();

..and then I want to add references (?) to this object in two different vectors;

Vector v1 = new Vector();
Vector v2 = new Vector();
v1.add(newPlayer);
v2.add(newPlayer);

The second vector (v2) doesn't seem to work when i try to get the player like:

Player tempPlayer = (Player)v2.get(0);

..but the first one seems to work (v1).. What the heck is happening? I'm not sure how the add-thing works, but it seems like the object (newPlayer) is _inserted_ in v1, and then when I add newPlayer to v2, there is only rubbish left! :) Why is that? What does add() do? Add the object itself or a reference to the object?

Thanx in advance!

/Mr.Stupid..
 
Old November 18th, 2003, 11:18 AM
Registered User
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm sorry guys, I'm retarded!
I used .toString() instead of .getName()

Like I said: /Mr.Stupid..
 
Old February 23rd, 2005, 01:04 AM
Registered User
 
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

MR. stupid
There is no problem in your code.I write it as u write and get perfact result.
this is my code
public class Learn
{
    public static void main(String args[])
    {
         play p = new play();
         Vector v1 = new Vector();
         Vector v2 = new Vector();
         v1.add(p);
         v2.add(p);
         play p2 = (play)v1.get(0);
         p2.method2();
         System.out.println("The vectro 1's get method called");
         play p3 = (play)v2.get(0);
         p3.method2();
         System.out.println("The vectro2 's get method called");
         System.out.println("method ends");

    }
}
class play
{
    play()
    {
        System.out.println("constructor calles");
        method2();
    }
    public void method2()
    {
        System.out.println("method 2 calles");
    }
}/////
/* it gives this out put */
/*
constructor calles

method 2 calles

method 2 calles

The vectro 1's get method called

method 2 calles

The vectro2 's get method called

method ends
*/
so please don't get confused.





Similar Threads
Thread Thread Starter Forum Replies Last Post
help add, delete from a JList in a Vector egolileopablo J2EE 1 January 1st, 2005 06:16 AM
LNC Add-in object reference problem srichart BOOK: Expert One-on-One Access Application Development 2 July 8th, 2004 09:59 PM
Vector.contains(Object obj) jacob J2EE 2 January 10th, 2004 02:34 PM
vector.add(); chikodi Servlets 2 October 23rd, 2003 02:03 AM
help me add, delete from a JList in a Vector egolileopablo Java GUI 0 October 22nd, 2003 09:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.