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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:42:11+00:00 2026-06-04T23:42:11+00:00

I want to write a class for mathematical Vectors (holding real numbers). I figured

  • 0

I want to write a class for mathematical Vectors (holding real numbers). I figured that Vector operations are pretty much the same regardeless of the dimension of the vector, so instead of writing classes like Vector2D, Vector3D, Vector4D, … I just want to write a Vector class.

Now the problem is that I can’t multiply a 2D vector with a 4D one, so I thought about a field dimension. But now I had to check it for every operation so I asked myself if I can do better than that. This is were generics came to my mind. But then again I have to do something like Vector<? extends Dimension>, where Dimension is simply a base class of Dimension.One, Dimension.Two and so on, which means, I have to write a Dimension class for every dimension I want to use vectos in.

So my question:

Is there a way of writing one class for vectors of arbitrary dimension without having to check the dimension at runtime?

  • 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-06-04T23:42:14+00:00Added an answer on June 4, 2026 at 11:42 pm

    If I understand your question correctly, then the answer is that you can’t have both. Either you use the type system to ensure correct dimensionality, and then you end up with a proliferation of explicit types (generics won’t help you), or you use state to track the dimensions and perform dynamic checks every time you perform an operation.
    Since the “dimension” of a vector is how many elements it holds, this will be represented somehow in the underlying data structure. For example if you use a list to store the values in the vector, the list knows how many elements it contains. So doing a simple runtime check is cheap and you can throw an exception when dimensions don’t match. That solution is much more flexible and easy to program with than a type based solution.

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

Sidebar

Related Questions

I want to write a class that manages euclidean vector and that store its
I want to write a C++ class that offers set operations that work on
I want to write a simple class (PHP5) that can 'run' an unknown amount
As part of some WSGI middleware I want to write a python class that
I want to write a 'Date' class that behaves like a Value Type. for
I want to write a container class that acts like a dictionary (actually derives
I want to write a java class that can be instantiated only 5 times
I want to write a class library that works in ASP.NET and standalone applications.
I want to write a generic class that should be casted to itself with
Suppose that I want to write a case class Stepper as follows: case class

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.