简体   繁体   English

我如何每天一次遍历数组?

[英]How can I iterate through an array once per day?

So I'm working on an application that needs to display a value, a specific string of text once per day. 因此,我正在开发一个需要每天显示一次值,特定文本字符串的应用程序。 I have all my strings in an array and now I just need a way to increase the index once per day. 我将所有字符串都放在一个数组中,现在我只需要一种每天增加一次索引的方法。 The kicker is that if the user downloads the application later in the year I need to have all the other days accounted for. 最重要的是,如果用户在当年晚些时候下载应用程序,则我需要考虑所有其他日子。 So basically the user will see the same tip as a person who downloaded the app on the first day. 因此,基本上,用户会看到与第一天下载该应用程序的人相同的提示。 Any suggestions? 有什么建议么?

Would using the Calendar class be my best bet? 使用Calendar类是我最好的选择吗? I just don't want to set an individual switch and case for every day of the year. 我只是不想为一年中的每一天设置一个单独的开关和保护套。

if I understand you correctly (Bob downloaded the app a hundred days ago should see the same tip as Alice who downloaded the application today), you can use Calendar's DAY_OF_YEAR value to display the same date 如果我对您的理解正确(Bob在100天前下载了该应用,应该会看到与今天下载该应用的Alice相同的提示),则可以使用Calendar的DAY_OF_YEAR值来显示相同​​的日期

    Calendar ca1 = Calendar.getInstance(); //get today's date

    int DAY_OF_YEAR=ca1.get(Calendar.DAY_OF_YEAR) -1; //DAY_OF_YEAR starts at one

    //avoid IndexOutOfBoundsExceptions
    String tip = tiparray[DAY_OF_YEAR % tiparray.length]; 

Not sure if I understand completely the question. 不知道我是否完全理解这个问题。 I think you will need to have the same tip shown for everyone in each day, right? 我认为您每天需要为每个人显示相同的提示,对吗?

If so, you can use Calendar.DAY_OF YEAR : 如果是这样,您可以使用Calendar.DAY_OF YEAR:

Calendar cal = Calendar.getInstance();
int index=cal.get(Calendar.DAY_OF_YEAR);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM