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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:58:29+00:00 2026-05-13T17:58:29+00:00

How can I store a 100K X 100K matrix in Java? I can’t do

  • 0

How can I store a 100K X 100K matrix in Java?

I can’t do that with a normal array declaration as it is throwing a java.lang.OutofMemoryError.

  • 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-13T17:58:29+00:00Added an answer on May 13, 2026 at 5:58 pm

    Sounds like you need a sparse matrix. Others have already suggested good 3rd party implementations that may suite your needs…

    Depending on your applications, you could get away without a third-party matrix library by just using a Map as a backing-store for your matrix data. Kind of…

    public class SparseMatrix<T> {
        private T defaultValue;
        private int m;
        private int n;
        private Map<Integer, T> data = new TreeMap<Integer, T>();
        /// create a new matrix with m rows and n columns
        public SparseMatrix(int m, int n, T defaultValue) {
            this.m = m;
            this.n = n;
            this.defaultValue = defaultValue;
        }
        /// set value at [i,j] (row, col)
        public void setValueAt(int i, int j, T value) {
            if (i >= m || j >= n || i < 0 || j < 0) 
                throw new IllegalArgumentException(
                        "index (" + i + ", " +j +") out of bounds");        
            data.put(i * n + j, value);
        }
        /// retrieve value at [i,j] (row, col)
        public T getValueAt(int i, int j) {
            if (i >= m || j >= n || i < 0 || j < 0) 
                throw new IllegalArgumentException(
                        "index (" + i + ", " +j +") out of bounds");
            T value = data.get(i * n + j);
            return value != null ? value : defaultValue;
        }
    }
    

    A simple test-case illustrating the SparseMatrix’ use would be:

    public class SparseMatrixTest extends TestCase {
        public void testMatrix() {
            SparseMatrix<Float> matrix = 
                new SparseMatrix<Float>(100000, 100000, 0.0F);
            matrix.setValueAt(1000, 1001, 42.0F);
            assertTrue(matrix.getValueAt(1000,1001) == 42.0);
            assertTrue(matrix.getValueAt(1001,1000) == 0.0);        
        }   
    }
    

    This is not the most efficient way of doing it because every non-default entry in the matrix is stored as an Object. Depending on the number of actual values you are expecting, the simplicity of this approach might trump integrating a 3rd-party solution (and possibly dealing with its License – again, depending on your situation).

    Adding matrix-operations like multiplication to the above SparseMatrix implementation should be straight-forward (and is left as an exercise for the reader 😉

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

Sidebar

Ask A Question

Stats

  • Questions 302k
  • Answers 302k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The application can start the service with the help of… May 13, 2026 at 8:25 pm
  • Editorial Team
    Editorial Team added an answer Here you go. Perl's multi-dimensional arrays are really arrays of… May 13, 2026 at 8:25 pm
  • Editorial Team
    Editorial Team added an answer It's the Universal Selector, and will match any element. The… May 13, 2026 at 8:25 pm

Related Questions

Dropping my lurker status to finally ask a question... I need to know how
I have a large table and I'd like to store the results in Memcache.
I want to store the average values of some data that is occasionally generated
I'm designing an application that receives information from roughly 100k sensors that measure time-series
Currently we have thousands of Microsoft Word files, Excel files, PDF's, images etc stored

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.