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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:16:56+00:00 2026-05-22T12:16:56+00:00

we have two process classes which are almost identical except One is used to

  • 0

we have two process classes which are almost identical except One is used to process a float[] data and another is for a int[] data.

I am thinking to combine them into one. My original thinking is use generic for float[] and int[].

Java generic is new to me. So I wrote the following test method to see how generic works but I cannot make it working for me.

public <T> T caculate(T[] values)
    {
        T result;
        for(int i = 0; i < values.length; i++)
        {
            result = result + values[i];
        }

        result = result/(values.length);
        return result;       
    }

Anybody can modify above method for me? float and int is primitive type in Java. Probably above method is not right.

  • 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-22T12:16:57+00:00Added an answer on May 22, 2026 at 12:16 pm

    We have two process classes which are almost identical except One is used to process a float[] data and another is for a int[] data.

    Unfortunately, you can’t do that with generics. Your best options is to bite the bullet and code the two classes separately; i.e. no generics.

    In theory, you could try changing the arrays to Integer[] and Float[] and changing <T> to <T extends Number>. However:

    • This liable to involve changing other parts of your code.

    • It is liable to make your processing code slower.

    • It will probably be difficult to express your processing algorithm in a generic way using T extends Number. For a start, the Number interface doesn’t support arithmetic.


    I guess it would also be possible to solve this problem by wrapping the primitive arrays in some class that makes them look like arrays of (say) integers. But that is liable to give you problems with truncation, precision and so on … depending on the algorithms you are implementing. So this will only work in limited cases.

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

Sidebar

Related Questions

We have a vxWorks design which requires one task to process messages from two
I have two completely different forms in one template. How to process them in
I have implemented the two classes shown at http://tomcat.apache.org/tomcat-6.0-doc/aio.html which gives a messenger application
I have two ViewModel classes : PersonViewModel and PersonSearchListViewModel. One of the fields PersonViewModel
In a WCF service, I have two classes with the [DataContract] attribute. One of
I have two process and a shared memory zone, my workflow is like this.
I have two processes - a user process and a root-level LaunchDaemon. I'd like
I have two ASP.NET sites (they can not run in the same process) and
I have two running processes in Windows, and each process has a pipe to
I have two unrelated processes that use .NET assemblies as plugins. However, either process

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.