gpt4 book ai didi

java - 获取数组的索引

转载 作者:行者123 更新时间:2023-12-01 23:32:49 24 4
gpt4 key购买 nike

我有以下代码:

DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);
String[] suspendedDates = {
"Tuesday, January 1",
"Monday, January 21",
"Sunday, February 10",
"Tuesday, February 12",
"Wednesday, February 13",
"Monday, February 18",
"Sunday, February 24",
"Tuesday, March 26",
"Wednesday, March 27",
"Thursday, March 28",
"Friday, March 29",
"Monday, April 1",
"Tuesday, April 2",
"Thursday, May 2",
"Friday, May 3",
"Thursday, May 9",
"Wednesday, May 15",
"Thursday, May 16",
"Monday, May 27",
"Thursday, July 4",
"Wednesday, August 7",
"Thursday, August 8",
"Friday, August 9",
"Thursday, August 15",
"Monday, September 2",
"Thursday, September 5",
"Friday, September 6",
"Saturday, September 14",
"Thursday, September 19",
"Friday, September 20",
"Thursday, September 26",
"Friday, September 27",
"Monday, October 14",
"Tuesday, October 15",
"Wednesday, October 16",
"Thursday, October 17",
"Friday, November 1",
"Sunday, November 3",
"Tuesday, November 5",
"Monday, November 11",
"Thursday, November 28",
"Sunday, December 8",
"Wednesday, December 25"
};

下面的行进行比较,如果匹配则返回 true:

Arrays.asList(suspendedDates).contains(df.format(Calendar.getInstance(Locale.US).getTime()))

我想知道的是,如何检查它匹配哪个索引?

最佳答案

您可以使用 List 接口(interface)中的 indexOf 方法来实现此目的。由于 Arrays.asList(suspendedDates) 为您提供了一个 List,您可以使用以下代码:

int index = Arrays.asList(suspendedDates).indexOf(...)

如果未找到,将返回-1

顺便说一句,请考虑使用 Date 类,而不是将日期存储在字符串中。

关于java - 获取数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19084364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com