public void addElement(Object obj) Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity. This method is identical in functionality to the add(Object) method (which is part of the List interface). (Adds the specified component to the end of this vector).
Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity.
This method is identical in functionality to the add(Object) method (which is part of the List interface). (Adds the specified component to the end of this vector).
你传到dosth()方法中的v只是main()方法中v的一个参照而已,而这两个v指向同一个地址,因此,你在dosth()方法中改变的只是这个参照的地址,而main()方法中的v还是指向原来的地址。所以得到上述结果。
import java.util.*;class A {
void dosth(Vector v)
{
v= null;
}
public static void main(String[] args)
{
A a = new A();
Vector v = new Vector();
v.addElement("ABC");
a.dosth(v);
System.out.println(v);
}
}
结果[ABC]import java.lang.*;
import java.util.*;class A {
void dosth(Vector v)
{
v= null;
}
public static void main(String[] args)
{
A a = new A();
Vector v = new Vector();
v.addElement("ABC");
v= null;
//a.dosth(v);
System.out.println(v);
}
}
结果null
这是为什么。谢谢
import java.lang.*;
import java.util.*;class A {
void dosth(Vector v)
{
//v.addElement("123");
v= null;");---------------对指针进行操作,一开始这个v指向main()里的v,现在指向空;但不影响main()里的那个v。
}
public static void main(String[] args)
{
A a = new A();
Vector v = new Vector();
v.addElement("ABC");
a.dosth(v);
System.out.println(v);
}
}System.out.println(v)打印的是什么内容。
如果改成
import java.lang.*;
import java.util.*;class A {
void dosth(Vector v)
{
v.addElement("123");——————对对象进行操作,这里的v和main()里的v共同指向一个内存地址,该地址里存放着一个Vector,里面有“abc”和“123”
//v= null;
}
public static void main(String[] args)
{
A a = new A();
Vector v = new Vector();
v.addElement("ABC");
a.dosth(v);
System.out.println(v);
}