-1

I'm implementing a timetable for a scholar use. I'd like to have a String [] object that has ALL the subjects erogated in a week. I thinked to append every String [] dailySubjects (that I have) to aString [] object (that we can call weekSubjects). In logical plan it's simple to see. But I've tried to implement and I'm having no results. How can I do? there is not an add method for String[]. That method would make all easier. By the way, My code

public Class getWeekSubjects () {
    public getWeekSubjects () {
        //this is a String [] composed by all the subject in a day
        String dailySubjects [] = new timetable().getDailySubjects;
    }
}
4

4 回答 4

1

我认为ArrayList最适合您的情况。此外,您应该在追加时使用StringBuilder 。

于 2013-09-16T16:11:28.860 回答
1

数组有固定的长度。您可以 (a) 使用ArrayList没有固定长度的 ,或 (b) 创建一个足够大的新数组以容纳所有数据并用于System.arraycopy()复制您的值。

于 2013-09-16T16:11:44.823 回答
1

如果我理解正确,您有一种获取每日科目的方法,现在您想将所有周科目放在一个数组中。

但是如果每天的科目重复呢?(周一数学,周二数学。)在这种情况下,你会想要一个集合,而不是一个数组。

public String[] getWeekSubjects() {
    Set<String> weekSubjects = new HashSet<String>();
    String[] dailySubjects = new timetable().getDailySubjects();
    Collections.addAll(weekSubjects, dailySubjects);

    // Add the other daily subjects... (somehow)    
    return weekSubjects.toArray(new String[0]);
}

顺便说一句,你发布的代码太可怕了......如果你的实际代码是这样的,那么你有更大的问题需要处理。

于 2013-09-16T16:12:54.723 回答
0

为什么不使用 aList<String>或其他东西,您必须使用硬性要求吗String[]

于 2013-09-16T16:11:51.513 回答