Java 谁能告诉我我';我是否使用正确的代码打印此ArrayList中的引用?
好的,我要做的是遍历名为DateArray的ArrayList,搜索该列表中的特定项,并输出该项出现的次数。当我在主类中测试它时,它只输出数字0,不管我输入的字符串是什么日期。这里是方法所在类的部分。我不认为现在有必要包括整个类,除非它令人困惑Java 谁能告诉我我';我是否使用正确的代码打印此ArrayList中的引用?,java,find-occurrences,Java,Find Occurrences,好的,我要做的是遍历名为DateArray的ArrayList,搜索该列表中的特定项,并输出该项出现的次数。当我在主类中测试它时,它只输出数字0,不管我输入的字符串是什么日期。这里是方法所在类的部分。我不认为现在有必要包括整个类,除非它令人困惑 public ArrayList<String> getTicketDates(){ int i; for (i=0; i <tickets.size(); i++){
public ArrayList<String> getTicketDates(){
int i;
for (i=0; i <tickets.size(); i++){
if(tickets .get(i).getPurchased()== false){
theDateArray.add(tickets.get(i).getDate());
}
}
for(int f=0; f<theDateArray.size();f++){
System.out.println(theDateArray.get(f)+ " ");
}
return theDateArray;
}
public int getTickets(String date){
int tix= theDateArray.indexOf(date);
int occurrences= Collections.frequency(theDateArray, tix);
if (tix>=0){
System.out.println(occurrences);
}
return occurrences;
}
这是我得到的输出
The dates in which tickets are available are as follows:
2017-06-09
2017-06-09
0
我希望0是2 我认为这些是你的错误:
int tix= theDateArray.indexOf(date);
int occurrences= Collections.frequency(theDateArray, tix);
您正在查找日期索引的频率,因此在您的示例中,2017-06-09
date的第一个索引可能是索引0?所以当你做收集时,频率。。。您正在查找日期数组中0
的频率,它返回0(无发生)
我会试着把它换成
// find the number of occurrences of your passed in date within the date array
int occurrences = Collections.frequency(theDateArray, date)
// find the number of occurrences of your passed in date within the date array
int occurrences = Collections.frequency(theDateArray, date)