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

  • SEARCH
  • Home
  • 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 7402365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:43:31+00:00 2026-05-29T04:43:31+00:00

I tried to Google for an answer but I think I am using wrong

  • 0

I tried to Google for an answer but I think I am using wrong keywords. What I am trying to do is – I have a collection of numbers (ints), and as I add new numbers I am computing the new average. I noticed that as my array grows, once I get to a certain count of elements (lets say 200), the calculation time becomes noticeable. I was wondering if there are any built-in SDK functions I can utilize to speed up my codes performance? I will be running the code on Android.

int[] numbers = new int[3];

private int average(int number){
    //some buildin operation to push in array an int?
    for(int i=0; i < numbers.length -1 ; i++){
        numbers[i]=numbers[i+1];
    }
    numbers[numbers.length -1] = number;
                    ;                
    //numbers[0] = numbers[1];
    //numbers[1] = numbers[2];
    //numbers[2] = number;

    int sum = 0;

    //any operation to get average?
    for(int i=0; i < numbers.length ; i++)
        sum = sum + numbers[i];

    //calculate average value
    double average = sum / numbers.length;

    return (int)average;
 }
  • 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-29T04:43:32+00:00Added an answer on May 29, 2026 at 4:43 am

    Now I just made ​​myself a circular buffer (as suggested Oli Charlesworth).

    And this is my implementation:

    /** Circular buffer */
    private class WindowBuffer {
        private final float [] values;
        private final int capacity;
        private float sum;
        private float average;
        private int head = -1;
        private boolean full = false;
    
        WindowBuffer(final int capacity) {
            this.capacity = capacity;
            values = new float[capacity];
            for (int i = 0; i < capacity; i++) {
                values[i] = 0f;
            }
        }
        void clean() {
            if (head == -1) return;
            for (int i = 0; i < capacity; i++) {
                values[i] = 0f;
            }               
            sum = 0;
            average = 0;
            head = -1;
            full = false;
        }
    
        void put(final float value) {
            head++;
            if (head >= capacity) {
                head = 0;
                full = true;
            }
            sum -= values[head];
            sum += value;
            values[head] = value;
            calculateAverage();
        }
    
        private void calculateAverage() {
            if (full) {
                average = sum / capacity;
            } else {
                average = sum / (head + 1);
            }
        }
    
        float getAverage() {
            return average;
        }
    }
    

    UPD

    void clean() {
                if (head == -1) return;
                if (full) {
                    for (int i = 0; i < capacity; i++) {
                        values[i] = 0f;
                    }
                } else {
                    for (int i = 0; i <= head; i++) {
                        values[i] = 0f;
                    }
                }
                sum = 0;
                average = 0;
                head = -1;
                full = false;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to find how to create DLL-s on linux using google, but
Have tried searching google but I'm struggling to find an answer to this. I
I've seen this syntax show up, and have tried to google for it's definition
I tried to find the difference on Google. BUT I 'm not able to
So, this seemed simple at first, but after crawling Google and here, the answer
Pretty self-explanatory, I tried google and got a lot of the dreaded expertsexchange, I
Ok... I tried google and didn't get many hits. I dont want to abuse
I tried to configure google adsense on my drupal site. I used two methods.
Tried following the instructions here: How to use Google app engine with my own
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return

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.