如何从特定索引启动ArrayList?

时间:2016-12-09 06:22:45

标签: android arraylist android-recyclerview

如果我有一个

的ArrayList
Sun
Mon
Tue
Wed
Thu
Fri
Sat

然后,今天是星期四,所以我希望它看起来像

Fri
Sat
Sun
Mon
Tue
Wed
Thu

我正在使用recyclerView来显示工作日的arrayList。请帮帮我。

4 个答案:

答案 0 :(得分:2)

试试这个

    int todayIndex = list.indexOf("Thu");
    int index = (todayIndex+1)%7;
    while(index!= todayIndex) {
        System.out.println(list.get(index));
        index = (index+1)%7;
    }

答案 1 :(得分:0)

是的,你可以这样做

list.add(1, "Fri")
list.add(2, "Sat")
list.add(3, "Sun")
list.add(4, "Mon")

答案 2 :(得分:0)

List接口提供subList方法。你应该可以使用它:

int tomorrowIndex = 1 + weekDays.indexOf("Thu");
if (tomorrowIndex < weekDays.size()) {
    List<String> previousDays = weekDays.subList(0, tomorrowIndex);
    weekDays.addAll(previousDays);
    previousDays.clear();
}

或者从两个子列表中构建一个新的List

List<String> newOrder = new ArrayList<String>(weekDays.size());
int tomorrowIndex = 1 + weekDays.indexOf("Thu");
if (tomorrowIndex < weekDays.size()) {
    newOrder.addAll(weekDays.subList(tomorrowIndex, weekDays.size());
}
newOrder.addAll(weekDays.subList(0, tomorrowIndex);

答案 3 :(得分:0)

嗨测试代码

                ArrayList<String> updatedList =new ArrayList<String>();
                ArrayList<String> newList =getList();
                int todayIndex = newList.indexOf("Thu")+1;
                if(todayIndex<newList.size()){
                    List<String> firstPart = newList.subList(0, todayIndex);
                    List<String> secondPartPart =  newList.subList(todayIndex, newList.size());         
                    updatedList.addAll(secondPartPart);
                    updatedList.addAll(firstPart);
                }else{
                    updatedList =newList;
                }
                mRootView.setAdapter(new FakePageAdapter(updatedList));

如果不能让我知道