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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:09:30+00:00 2026-06-06T04:09:30+00:00

Okay, so I’m making a dynamic 2D array in Java that implements the java.util.Collection

  • 0

Okay, so I’m making a dynamic 2D array in Java that implements the java.util.Collection interface. I made my array implement it because I wanted it to have the same functionality as a normal Collection. However, I cannot implement the size() method because in the interface it returns an integer and a 2D matrix could potentially overflow an integer type.

Here’s a snippet of my class I’m trying to make:

public abstract class AbstractMatrix<E> implements Collection<E>{
     @Override
     public long size() {
         return columns * rows;
     }
}

Now, this won’t work because “The return type is incompatible with Collection<E>.size()“, and if I change the type to int, columns * rows could overflow.

I know I can’t override the size method like this, but is there any way I can make sure the method returns the correct size while still implementing the Collection interface?

Yes, I know this is impractical and will likely never be an issue, but I was interested to know if there was a good solution for it.

  • 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-06T04:09:32+00:00Added an answer on June 6, 2026 at 4:09 am

    Although your implementation of size is questionable, the contract for Collection#size is defined in the javadoc:

    Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

    So you could calculate the size as a long, and return Integer.MAX_VALUE if it is larger than Integer.MAX_VALUE.

    Alternatively, you could mimic the way it is implemented in LinkedList#add for example, where size is simply incremented and is allowed to overflow.

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

Sidebar

Related Questions

Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay, so I'm working on a java server for an Apps backend, it must
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay, so I'm trying to make a game that uses this algorithm: http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection But
Okay so I made this program to help me out with my homework and
Okay, I haven't used Interface Builder since Xcode 3.x but I remember there being
Okay, I have hundreds of .net controls with text attributes that needs to be
Okay so i just wanted to put a little search form on my site
Okay so what I have is a table that keeps track of history on
Okay, I know I have to be doing something wrong here because the performance

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.