How can we get the last added element in ArrayList. I find this that explain how to get the last element, but is the last added element always the last element ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes for
ArrayList, It preserves the order of insertionIf you explicitly add the element at particular position by specifying index
add(), in this case you need to set insertion time by customizing ArrayList implementation and while retrieving the latest inserted element consider that time in calculationor better have a reference pointing to last inserted item as Marko Topolnik suggested, also maintain it on removal
Better thing would be use
LinkedHashSet, if you are not concerned about uniqueness property of set