Java 最近条目的ArrayList逻辑?
基本上,我有一个名为“recentContacts”的Java 最近条目的ArrayList逻辑?,java,android,arraylist,Java,Android,Arraylist,基本上,我有一个名为“recentContacts”的ArrayList,并且我将条目数量限制为10个 现在,我正在尝试获取ArrayList以替换第一个索引中的所有条目,列表已满 下面是一些代码来演示 // Limits number of entries to 10 if (recentContacts.size() < 10) { // Adds the model
ArrayList
,并且我将条目数量限制为10个
现在,我正在尝试获取ArrayList
以替换第一个索引中的所有条目,列表已满
下面是一些代码来演示
// Limits number of entries to 10
if (recentContacts.size() < 10)
{
// Adds the model
recentContacts.add(contactsModel);
}
else
{
// Replaces model from the 1st index and then 2nd and 3rd etc...
// Until the entries reach the limit of 10 again...
// Repeats
}
//将条目数限制为10
如果(recentContacts.size()<10)
{
//添加模型
recentContacts.add(contactsModel);
}
其他的
{
//从第一个索引、第二个索引和第三个索引等替换模型。。。
//直到条目再次达到10的限制。。。
//重复
}
注意:上面的if语句只是一个简单的示例,可能不是解决问题的正确方法
实现这一目标最简单的方法是什么?谢谢 您必须维护要替换的下一个元素的索引。 实际上,您甚至可以在
ArrayList
已满之前使用该索引
例如:
int index = 0; // initialize the index to 0 when the ArrayList is empty
...
recentContacts.add(index,contactsModel);
index = (index + 1) % 10; // once index reaches 9, it will go back to 0
...
您必须维护要替换的下一个元素的索引。 实际上,您甚至可以在
ArrayList
已满之前使用该索引
例如:
int index = 0; // initialize the index to 0 when the ArrayList is empty
...
recentContacts.add(index,contactsModel);
index = (index + 1) % 10; // once index reaches 9, it will go back to 0
...
研究使用,特别是查看
LinkedHashMap#RemoveedStantry()
。你需要有人为你做简单的逻辑吗?简单是主观的。我们都有不同的技能水平。:)研究使用,特别是查看LinkedHashMap#RemoveedStantry()
。你需要有人为你做简单的逻辑吗?简单是主观的。我们都有不同的技能水平。:)谢谢很有魅力,谢谢!工作起来很有魅力。