Sunday, 18 August 2013

What's wrong with this deletion call

What's wrong with this deletion call

Attached is a sample code, I am trying to clear old content of the list
before filling it with new data but it doesn't seem to be working. Let's
say if in first call I got list of size 4 and in second call size of 6,
then it's making the list grow to size of 10. What I want is only data
from most recent call.
public class DemoClass
{
List<String> myList = new ArrayList<>();
public void doSomething()
{
myList = getMyList("params");
System.out.println(myList.toString());
//Results in ArrayList of size 3 = AAA, BBB, CCC
}
public void doAnotherThing()
{
myList = getMyList("different params"); // Actual size returned is 4
System.out.println(myList.toString());
//Results in ArrayList of size 7 = AAA, AAA, BBB, BBB, CCC, DDD, EEE
}
public List<String> getMyList(String params)
{
//Some business Logic based on params
myList.clear();
myList = //code to populate the list
return myList;
}
}

No comments:

Post a Comment