Make array container classes behave like ArrayList

How to make a class that contains a variable that stores an array able to return the inner element holder like elementData in ArrayList. For example when we want to iterate in enhanced for loop (var e : elements) where elements is an ArrayList , it somehow converts the elements to array to be iterated. I couldn't find documentations online or find discussions about it. Using toArray doesn't seem to work
10 Replies
JavaBot
JavaBot12mo ago
This post has been reserved for your question.
Hey @circle! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st12mo ago
ArrayList doesn't return an array when iterating over it with a for each loop what exactly are you talking about?
circle
circleOP12mo ago
Hmm. When extending a class with ArrayList, using this method would retrieve the #elementData of itself (or a variable) it's pretty interesting
dan1st
dan1st12mo ago
can you show an example of what you mean? toArray gives you a copy if the data as an array
circle
circleOP12mo ago
This would compile. Sorry for the boobs
No description
circle
circleOP12mo ago
kind of means, that this actually retrieves the ArrayList#elementData right?
dan1st
dan1st12mo ago
no the foreach loop is equivalent to
for(Iterator<Song> it = this.iterator(); it.hasNext();){
Song song = it.next();
//...
}
for(Iterator<Song> it = this.iterator(); it.hasNext();){
Song song = it.next();
//...
}
which is the case for all foreach loops that don't operate on arrays directly
circle
circleOP12mo ago
I understand the case with for loop now, it uses an iterator. Thanks
JavaBot
JavaBot12mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot12mo ago
Post Closed
This post has been closed by <@389034898666553344>.
Want results from more Discord servers?
Add your server