Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6371539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:05:08+00:00 2026-05-25T01:05:08+00:00

Am having an arraylist and its code is: ArrayList<String> recentdata=new ArrayList<String>(); I want to

  • 0

Am having an arraylist and its code is:

ArrayList<String> recentdata=new ArrayList<String>();

I want to limit the size of ArrayList to a specific size i.e. 5 in my case, when it reaches to that number.
That means 0 to 5 it works as ArrayList, when it reaches to that 5, I want to stop the growth of ArrayList.

For that am using: to set the size

if(recentdata.size() >= 5) {
    recentdata.trimToSize();
}

but its not working i.e., size growth is not stopping it goes 6,7 etc.

How to do this? anyother way to do this? or what is wrong in my approach?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:05:09+00:00Added an answer on May 25, 2026 at 1:05 am

    trimToSize doesn’t remove any element from the list. From the javadoc:

    Trims the capacity of this ArrayList instance to be the list’s current
    size. An application can use this operation to minimize the storage of
    an ArrayList instance.

    The capacity is the size of the array used by the ArrayList internally. If your list contains 6 elements and has a capacity of 10, the last 4 elements of the array are null. Calling trimToSize will just make the list use an array of 6 instead of 10.

    Just don’t add anything to the list once its size is 5, instead of adding, and then trying to remove.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am having a string arraylist 'names'.which contains names of people.i want to sort
I'm having trouble getting the right number of elements in the ArrayList alt in
Here is a piece of code that I've written. The problem that I'm having
I am having this problem that I had used Arraylist in my entire program
I am having this problem in below code as my ArrayList both name and
I'm writing some code that needs to process an arbitrary number of lists of
Having to upgrade a database schema makes installing a new release of software a
Having a problem getting a TreeView control to display node images. The code below
I'm having difficulty trying to remove a div with a particular ID, and its
I'm having tough time figure out how to display object's properties in ArrayList using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.