0

I'm working on an Android project and I have a problem with creating objects dynamically.

For example, I want the events named like this:

*event1

*event2

...

I'm used to work in PHP, and I know I can't do this the same way in Java because the code has to be compiled before the program starts.

I don't know how many objects the program will have to make, as the data comes from an online database. I'm able to create 1 event if I put something like this:

Event event = new Event();

I would really like something to make an object for each event and store each object in a db4o database.

As I said, it's possible that there is only one event, but it is also possible that there are 100 events.

1 Answer 1

7

It sounds like you need a collection of events. Try putting them in a simple data structure like an ArrayList (similar to an array, but it can expand in size at run time). This way you won't have multiple variables event1, event2, etc., but just one variable named events (or something similar) that holds as many objects as you need.

Sign up to request clarification or add additional context in comments.

3 Comments

ok, I'll check it out. do you know if it is possible then to give each object a different name? as I will have to take them out of the array after and store them seperately in the db4o database. I'll try anyway.
@pieter You'd be able to take them out and store them one at a time, so there would be no need to have different variable names for each one. You could just re-use the same variable name.
ok thank you , it seems like it's working. sorry if this was a stupid question, but just learning to work with it !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.