• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a Method that adds elements of another Class to an array  RSS feed

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,
I'm currently learning OOP (Object Oriented Programming) and for this purpose have an assignment that is quite complex (for me at least).
I have already made some progress but this topic stumped me.
The following two classes are concerned:


And



In the TimeTable class I have to creat the method "add" that receives lect of type Lecture as a parameter.
This method is supposed to add a lecture to the timetable.
Im not sure where to begin, I assume that the Array in TimeTable is supposed to be an Array of Objects that holds objects of Type Lecture.
How do I add to this array?
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep track of how many entries has already been added and add to next available position in the array. If the array is already full copy its contents to a new bigger array and then add new entry.

Or use an ArrayList and do not worry about all that stuff. Just invoke add method.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that a way?
 
Greenhorn
Posts: 25
AngularJS Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the List Implementations tutorial by Oracle.

As you can see, there are several advantages on using ArrayList over simple Arrays.

Combining this with generics you can create a list who accepts only one kind of object!

Ex:

This code would output "One more object" twice.

Then you can easily use add() and remove() methods and some others provided by ArrayList class. You can even use size() to get the number of elements and eliminate the use of numberOfEntries!

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
I would use Arraylist if I could, but the assignment requires a regular array.
 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not sure where to begin, I assume that the Array in TimeTable is supposed to be an Array of Objects that holds objects of Type Lecture.
How do I add to this array?


How would you add to any array?

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, that's a start. First thing, I gave you a (badly named) variable i that you reused in the for loop. You can't do that. Second, you want to add to the array but you are setting parameter. How would set the i array? Third, you are using a for loop as if you are setting several variables. But you only to set one and move the index. Let's try with this code:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!