1

I am dealing with adding objects to lists and have faced some problems while assigning new values to temporary object. I've come up with myself and found here an answer, but I can't add comments and my question is: is it a good practice to create a new instance of a class each time I want to add an element in a loop? What about the memory usage?

1 Answer 1

1

When you add objects to your list, are these new objects, or are you pulling existing ones from some other list or data structure? If it's the former (they are new), then you must construct them as you add them (new instances).

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

1 Comment

I iterate over some data string to extract data and assing it to an object and then to a list. It will look similarily to this: for(int i = 0; i < myList.size(); i++){ temporary = new MyObject(); temporary = extractData(); myList.add(temporary.temporaryField) }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.