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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:31:56+00:00 2026-05-26T02:31:56+00:00

I’ve got another question with List of lists. Once again, I have the generic

  • 0

I’ve got another question with List of lists. Once again, I have the generic matrix class as below.

public class Matrix<T>
{
  List<List<T>> matrix;

  public Matrix()
  {
     matrix = new List<List<T>>();
  }

  public void Add(IEnumerable<T> row)
  {
     List<T> newRow = new List<T>(row);
     matrix.Add(newRow);
  }
}

// Test code
Matrix<double> matrix = new Matrix<double>();    
matrix.Add(new List<double>() { 0, 0 });
matrix.Add(new List<double>() { 16.0, 4.0 });

I’m reading lines of strings from a text file which contains values with the following format,

4 2

0.5 0.4 0.6 0.1 10.1 11.1 0.5 12.0

The first line specifies the 4×2 matrix size.
The second line needs to be so that first 4 values are in the first column of the matrix and the last four values should be in the second column.
This is dynamic hence the size is not fixed.

Reading the lines and delimiting these lines is sorted. My question is around how to use the Matrix class to store these values. In other words how can I do the elements in a row ?

It should be done so that the matrix will look like,

0.5 10.1

0.4 11.1

0.6 0.5

0.1 12.0

Thanks in advance.

  • 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-26T02:31:56+00:00Added an answer on May 26, 2026 at 2:31 am

    Should your matrix dimensions really be mutable? A better alternative might be to pass its dimensions inside constructor and allocate arrays inside the matrix:

    public class Matrix<T>
    {
        private readonly T[][] _matrix;
    
        public Matrix(int rows, int cols)
        {
            _matrix = new T[rows][];
            for (int r = 0; r < rows; r++)
                _matrix[r] = new T[cols];
        }
    
        public T this[int r, int c]
        {
            get { return _matrix[r][c]; }
            set { _matrix[r][c] = value; }
        }
    }
    

    Alternatively, it it does need to be mutable, you can choose to allocate it “lazy”, as needed:

    public class Matrix<T>
    {
        private readonly List<List<T>> _matrix;
    
        public Matrix()
        {
            _matrix = new List<List<T>>();
        }
    
        public T this[int r, int c]
        {
            get
            {
                ResizeIfNeeded(r, c);
                return _matrix[r][c]; 
            }
            set
            {
                ResizeIfNeeded(r, c);
                _matrix[r][c] = value;
            }
        }
    
        private void ResizeIfNeeded(int row, int col)
        {
            while (_matrix.Count <= r)
                _matrix.Add(new List<T>());
    
            var row = _matrix[r];
            while (row.Count <= c)
                row.Add(default(T));
        }
    }
    

    Note that the second approach might do a lot of allocations if you are populating it sequentially.

    I would opt for the first approach (a fixed size matrix), since in most cases its dimensions are already known. It is fastest to allocate, and it’s safe to use in a multithreaded application.

    If you are reading values from a file, setting them through indices should much simpler than instantiating a new list for each row.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.