473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

question about ArrayList

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
3 8817
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
3
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to...
1
by: Irfan Akram | last post by:
Dear Friends, I'll be glad if you guys could help out. I am currently doing a web project in asp.net using c#. An ArrayList object in my code contains element records containing 3 fields. In a...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
9
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
11
by: Simon Says | last post by:
Hi all, I've an arraylist A1 that contains {0,1,2,3,4,5,9,8,7} I've another arraylist A2 that contains {4,5,9} I would like to search whether if A2 is present in A1 and maybe return the first...
4
by: dotnetnoob | last post by:
i have a arraylist that look like this item 0 - hold this value 1,2,4 item 1 - hold this value 3,6,7 if i want to access the item 0 i get the whole 1,2,4 how do i seperate them out like i...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.