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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:11:11+00:00 2026-05-25T12:11:11+00:00

public class Matrix<TValue, TList extends List<E>> { private TList<TList<TValue>> items; } I want to

  • 0
public class Matrix<TValue, TList extends List<E>> {
    private TList<TList<TValue>> items;
}

I want to use 2 instances of Matrix class. One with ArrayList<Integer> and second with LinkedList<Integer>.

  • 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-25T12:11:12+00:00Added an answer on May 25, 2026 at 12:11 pm

    Unfortunately, it is very difficult to code a generic object wich contains a list of lists the way you want to.

    This is because of type erasure in java wich means:

    LinkedList<Integer> ll = new LinkedList<Integer>();
    assert(ll.getClass() == LinkedList.class); // this is always true
    
    LinkedList<String> ll_string = new LinkedList<String>();
    assert(ll.getClass() == ll_string.getClass()); // this is also always true
    

    However, if the types of lists you want to use is small, you can do something similar to this example (this one is limited to ArrayList and LinkedList):

    public class Matrix <TValue> {
    
        Object items = null;
    
        public <TContainer> Matrix(Class<TContainer> containerClass) throws Exception{       
            try{
                TContainer obj = containerClass.newInstance();
    
                if(obj instanceof ArrayList){
                    items = new ArrayList<ArrayList<TValue>>();
                } else if(obj instanceof LinkedList){
                    items = new LinkedList<LinkedList<TValue>>();
                }                                 
            }catch(Exception ie){
                throw new Exception("The matrix container could not be intialized." );
            }                       
            if(items == null){
                throw new Exception("The provided container class is not ArrayList nor LinkedList");
            }
        }
    
    
        public List<List<TValue>> getItems(){
            return (List<List<TValue>>)items;
        }
    
    
    }
    

    This can be easily initialized and used:

    try {
            Matrix<Integer> m_ArrayList = new Matrix<Integer>(ArrayList.class);
            Matrix<Integer> m_LinkedList = new Matrix<Integer>(LinkedList.class);
        } catch (Exception ex) {
            ex.printStackTrace();;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Item { ... } public class Order { public List<Item> Items ...
This is my template matrix class: template<typename T> class Matrix { public: .... Matrix<T>
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE;
I'm writing a class to represent a matrix. I want it to look something
what's wrong with this code? class matrix{ private: friend transpose(const matrix&); friend class invert;
I want to use partial specialization for a template class so that all children
I'm trying to convert the following C# into F#: public class Matrix { double[,]
I have a 'matrix' object like follows: public class Matrix { public Dictionary<string, string>
I'm writing a generic matrix class in c++. I want to be able to

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.