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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:47:53+00:00 2026-05-18T10:47:53+00:00

I have a class Matrix which contains ArrayList<ArrayList<Double>> matrix; inside, and I want to

  • 0

I have a class Matrix which contains ArrayList<ArrayList<Double>> matrix; inside, and I want to know which is preferred when I want to copy instances of that class.

  1. Should I implement clone
  2. Use unmodifiable list HINT I don’t know how to do that on an ArrayList
  3. Do custom copyEntries(){return Matrix(this); , and a copy constructor (Matrix other)

P.S

  1. I’m flexible if there was a better type recommendation like vector of vector
  2. if List<List<Double>> and then new ArrayList<Double> is that better ?
  3. I’ve used Apache commons, and some other libs but at the end I found It should be custom made matrix class.
  • 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-18T10:47:54+00:00Added an answer on May 18, 2026 at 10:47 am

    If what you try to do is to copy the matrix, bear in mind Double is unmodifiable so, it doesn’t make much sense, creating new instances. You can freely copy the references using something like:


    List> copy = new ArrayList>();
    copy.addAll( original );

    Then you can modify any of the elements and the original won’t be changed:

    copy.get( 0 ) .get( 0 ) = -1.0; 
    
    // original at ( 0,0 ) remains the same ... 
    

    Ok, I get my compiler and created a running sample to probe my point of not cloning the values.

    import java.util.Arrays;
    import java.util.List;
    import java.util.ArrayList;
    
    public class CopyMatrix { 
        public static void main( String [] args ) { 
    
            Matrix m = new Matrix();
            m.add( Arrays.asList(0.0, 1.0, 2.0 ));
            m.add( Arrays.asList(3.0, 4.0, 5.0 ));
            m.add( Arrays.asList(6.0, 7.0, 8.0 ));
    
            System.out.println("m = \n" +  m );
    
            Matrix m1 = m.copy() ;
    
            m1.get( 0 ).set( 0 , 100.0 );
            m1.get( 2 ).set( 2 , -400.0 );
    
    
            System.out.printf( "After m = %n %s %n m1 = %n %s %n", m, m1);
        }
    }
    class Matrix extends ArrayList<List<Double>> {
        public Matrix copy() {
            Matrix copy = new Matrix();
            for( List<Double> each : this ) { 
                copy.add( new ArrayList<Double>( each ) );
            }
            return copy;
        }
    }
    

    Output:

     java CopyMatrix 
    m = 
    [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]
    After m = 
     [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]] 
     m1 = 
     [[100.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, -400.0]] 
    

    It works!!

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

Sidebar

Related Questions

I have a class which contains a few boost::numeric::ublas::matrix's within it. I would like
I have a class to parse a matrix that keeps the result in an
I have class method that returns a list of employees that I can iterate
I have class with a member function that takes a default argument. struct Class
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have a class Parameter, the purpose of which is to represent the possible
I have a class template<size_t N, size_t M> class Matrix { // .... };
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter

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.