By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,469 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,469 IT Pros & Developers. It's quick & easy.

question about ArrayList

P: n/a
roy
The following is a simple example I wrote.
I want function makenull() to set the arraylist null.
However, in this example, after coming back from makenull() function,
s is still not null.

I got confused. Anyone can give me some help?
public class test {
public static void makenull(java.util.ArrayList s){
s = null;
}
public static void main(String[] args) {
java.util.ArrayList s = new java.util.ArrayList();
test.makenull(s);
// after test.makenull function, s is NOT null
}
}

Thanks
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
public class MyApp {

public static void makenull(java.util.ArrayList c) {
// c - is a new refference on object of ArrayList class
// value of c refference is same as in s
// so you have two reffeneces on same object
c=null; // here you make c (one of this refference) as null
}

public static void main(String[] args) {
java.util.ArrayList s = new java.util.ArrayList();
MyApp.makenull(s);
//s - is still refference on object of ArrayList class and
//as result not null
}

}

roy wrote:
The following is a simple example I wrote.
I want function makenull() to set the arraylist null.
However, in this example, after coming back from makenull() function,
s is still not null.

I got confused. Anyone can give me some help?
public class test {
public static void makenull(java.util.ArrayList s){
s = null;
}
public static void main(String[] args) {
java.util.ArrayList s = new java.util.ArrayList();
test.makenull(s);
// after test.makenull function, s is NOT null
}
}

Thanks

Jul 17 '05 #2

P: n/a
Make 'makenull' a member function that accepts no arguements instead of
static with arguement and it will work. Can you see why? The reference s
that the body of makenull sees is a copy of the reference you created in
your main method. If you are familiar with C you might recall that for a
method to set a pointer to null it must receive a pointer to the pointer.

void makenull(**ptr) {
*ptr = null;
}

void main(int argc, char** argv) {
void *vPtr = malloc(16);
vPtr = makenull(&vPtr); // pass in the address of the pointer,
// not the pointer itself.
}
The only way I can think of to accomplish the same kind of behavior in
java is to wrap the reference inside of another reference.

public class Foo {

private String s;

public static void makeSnull(Foo outterRef) {
outterRef.s = null;
}

public static void main(String[] args) {
Foo foo = new Foo();
Foo.makeSnull(foo);
}
}
roy wrote:
The following is a simple example I wrote.
I want function makenull() to set the arraylist null.
However, in this example, after coming back from makenull() function,
s is still not null.

I got confused. Anyone can give me some help?
public class test {
public static void makenull(java.util.ArrayList s){
s = null;
}
public static void main(String[] args) {
java.util.ArrayList s = new java.util.ArrayList();
test.makenull(s);
// after test.makenull function, s is NOT null
}
}

Thanks

Jul 17 '05 #3

P: n/a
nice try... been there, done that...

remember java is not c++, things are done a wee bit differently...

while what dave has written will work, it's kinda going against the
grain...

here's a simple alternative, just a slight change in the coding style
that you are used to:
public class test { here->public static java.util.ArrayList makenull(java.util.ArrayList s){ for (int i=0; i<s.lenght; i++) here-> s[i]=null;
return s; }
public static void main(String[] args) {
java.util.ArrayList s = new java.util.ArrayList(); here-> s = test.makenull(s); // after test.makenull function, s is NOT null
}
}

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.