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 3751474
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:04:48+00:00 2026-05-19T09:04:48+00:00

I want to save integers in a data structure, but without knowing the number

  • 0

I want to save integers in a data structure, but without knowing the number
of integers i might get.
I want the database to be of FIFO kind.
What is best for this purpose?

  • 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-19T09:04:48+00:00Added an answer on May 19, 2026 at 9:04 am

    Apart from using a database, if you just have a number of intergers you could write them to a plain file. Plain files retain order, however removing entries can be expensive.

    You can write/rewrite 1 million integers in about 0.1 seconds using a plain file.

    An efficient collecton for int primitives is TIntArrayList. Its like @JPelletier’s suggestion but wraps a int[]. A million int values should take about 4 MB of memory or disk.

    EDIT: This shows that for 1 million numbers ArrayList is a bad choice. Mainly because remove(0) is O(n) rather than O(1)

    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    // based on http://www.cs.princeton.edu/introcs/43stack/RingBuffer.java.html
    public class IntRingBuffer {
        private final int[] a;       // queue elements
        private int N = 0;           // number of elements on queue
        private int first = 0;       // index of first element of queue
        private int last  = 0;       // index of next available slot
    
        // cast needed since no generic array creation in Java
        public IntRingBuffer(int capacity) {
            a = new int[capacity];
        }
    
        public boolean isEmpty() { return N == 0; }
        public int size()        { return N;      }
    
        public void enqueue(int item) {
            if (N == a.length) { throw new RuntimeException("Ring buffer overflow"); }
            a[last] = item;
            last = (last + 1) % a.length;     // wrap-around
            N++;
        }
    
        // remove the least recently added item - doesn't check for underflow
        public int dequeue() {
            if (isEmpty()) { throw new RuntimeException("Ring buffer underflow"); }
            int item = a[first];
            N--;
            first = (first + 1) % a.length;   // wrap-around
            return item;
        }
    
        public static void main(String... args) {
            int size = 1000000;
            {
            long start = System.nanoTime();
            IntRingBuffer list = new IntRingBuffer(size);
            for(int i=0;i< size;i++)
                list.enqueue(i);
            for(int i=0;i< size;i++)
                list.dequeue();
            long time = System.nanoTime() - start;
            System.out.println(list.getClass().getSimpleName()+": Took "+time/1000/1000+" ms to add/remove "+size+" elements");
            }
            {
            long start = System.nanoTime();
            List<Integer> list = new LinkedList<Integer>();
            for(int i=0;i< size;i++)
                list.add(i);
            for(int i=0;i< size;i++)
                list.remove(0);
            long time = System.nanoTime() - start;
            System.out.println(list.getClass().getSimpleName()+": Took "+time/1000/1000+" ms to add/remove "+size+" elements");
            }
            {
            long start = System.nanoTime();
            List<Integer> list = new ArrayList<Integer>();
            for(int i=0;i< size;i++)
                list.add(i);
            for(int i=0;i< size;i++)
                list.remove(0);
            long time = System.nanoTime() - start;
            System.out.println(list.getClass().getSimpleName()+": Took "+time/1000/1000+" ms to add/remove "+size+" elements");
            }
    
        }
    }
    

    Prints

    IntRingBuffer: Took 31 ms to add/remove 1000000 elements
    LinkedList: Took 252 ms to add/remove 1000000 elements
    ArrayList: Took 325832 ms to add/remove 1000000 elements
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to save and store simple mail objects via serializing, but I get
i've got some binary data which i want to save as an image. When
I am reading a file of integers. I want to save integers from each
I want to save the objects I generated in a program. After restart the
I want to open a save file dialog, have the user enter a filename,
I want to create a Java application bundle for Mac without using Mac. According
I want to create a component which can be used like: <mc:chart data=#{bean.data} width=200
I have a list which contains strings integers and floats. I want to convert
I have a variable that contains an integer which I then want to save
Want to know what the stackoverflow community feels about the various free and non-free

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.