附件是一个示例代码,我试图在用新数据填充列表之前清除列表的旧内容,但它似乎不起作用。假设在第一次调用中我得到了大小为 4 的列表,而在第二次调用中得到了大小为 6 的列表,那么它会使列表增长到 10 的大小。我想要的只是最近调用的数据。
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;
}
}