我正在实现一个 JTable,用户可以在其中定义 TimeTable。每个科目都有几个学分,我要数一周内所有学分的总和。显然,为此,有必要将所有重复的科目计算一次(我不会将同一科目的学分计算两次)。
例如,如果 JTable 是
我只想获得一次价值Math
, English
, Science
, Philosophy
, 。我尝试使用以下方法来做到这一点:Art
private void getOnce (String[] dailyLessons)
{
Set<String> weekSubjects = new HashSet<String>();
int weeklyCredits=0;
//dailylessons is a String[] that contains the lessons of the day
Collections.addAll(weekSubjects, dailyLessons);
//String[] week would contain every subject only one time
String [] week = weekSubjects.toArray(new String[0]);
//for all the subject I get its credits
for (int i=0; i<week.length; i++)
{
if (!week[i].equals("no"))
{
String [] credits= week[i].getCredits;
weeklyCredits += credits;
}
}
}
但它不起作用。你能解释一下为什么吗?我的代码的正确版本将不胜感激。