Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最近条目的ArrayList逻辑?_Java_Android_Arraylist - Fatal编程技术网

Java 最近条目的ArrayList逻辑?

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

基本上,我有一个名为“recentContacts”的
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()
。你需要有人为你做简单的逻辑吗?简单是主观的。我们都有不同的技能水平。:)谢谢很有魅力,谢谢!工作起来很有魅力。