• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList problem  RSS feed

 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class and an ArrayList inside the class. I am updating the arraylis from a method updateArrayList() like

updateArrayList(int x) {
this.dbList.add(x);
}

And in main() i am trying to access the same arraylist.

class testClass {
public ArrayList dbList;
public static void main(String args[]){
testCalss testObj = new testClass();
for(i = 0; i < 10; i++) {
testObj.updateArrayList(i);
}
testObl.dbList() --> //Here if I access the arraylist I get only the last element that I pushed and not the entire list (9)

Can anyone please explain what I am missing here.

Thanks,
Kashyap
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kashyap Hosdurga:
testObl.dbList() --> //Here if I access the arraylist I get only the last element that I pushed and not the entire list (9)


What exactly are you doing on that line? Post the exact code please.
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you doing on that line? Post the exact code please.

I am trying to get a ListIterator on te arrayList by following code

ListIterator tempLT = testClass.dbList.listIterator();
while( tempLT.hasNext())
{
//Access the arraylist and process
}

Please let me know if I am clear or if I need to post the remaining code.

Thanks,
Kashyap
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it is not testClass

ListIterator tempLT = testObj.dbList.listIterator();


Thanks,
Kashyap
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kashyap Hosdurga:
...Please let me know if I am clear or if I need to post the remaining code...

That might be a good idea, because there seems to be important pieces missing. There are some typos and syntax issues, but it looks like the issue you're describing (a problem accessing the List elements) is here...

But with only that comment line, we can't know for sure.
[ May 26, 2006: Message edited by: marc weber ]
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
class smallClass {
public int number;
}

class testClass {
public ArrayList dbList;
testClass() {
dbList = new ArrayList();
}

public static void main(String args[]){
testClass testObj = new testClass();
for(int i = 1; i < 10; i++) {
testObj.updateArrayList(i);
}
ListIterator LT1 = testObj.dbList.listIterator();
while( LT1.hasNext()) {
smallClass y = (smallClass) LT1.next();
System.out.println(y.number);//Here if I access the arraylist I get only the last element that I pushed and not the entire list (9)
}
}

public void updateArrayList(int x) {

smallClass I = new smallClass();
I.number = x;
System.out.println("Testing" + I);
for (int i =0; i <5 ; i++) {
I.number=I.number + i;
this.dbList.add(I);
}
}
}

The o/p i am expecting is
1,2,5,8,13,....
But if you can see the list has only last element from each iteration of the for loop inside "updateArrayList".

Thanks,
Kashyap
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the o/p is here
16 16 16 16 16 22 22 22 22 22 28 28 28 28 28 34 34 34 34 34 40 40 40 40
40 46 46 46 46 46 52 52 52 52 52 58 58 58 58 58 64 64 64 64 64
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the updateArrayList method, you are creating a new instance of smallClass, and then adding that object to the ArrayList 5 times. So each element in the ArrayList is a reference to the same instance of smallClass.

Along the way, you are reassigning new values to the instance variable "number." But because each element in the ArrayList is, in fact, the same instance (with the same instance variable), each "element" will have the same value for "number."
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc,
I changed the code to

public void updateArrayList(int x) {
for (int i =x; i < (x+5) ; i++) {
smallClass I = new smallClass();
I.number = x;
I.number = I.number + i;
System.out.println("==" + I.number);
this.dbList.add(I);

}
}

Now it is working fine.

Thank you all for your replies.

Kashyap
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!