Not able initialise ArrayList inside default constructorCreate ArrayList from arrayHow do I call one constructor from another in Java?Fastest way to determine if an integer's square root is an integerWhen to use LinkedList over ArrayList in Java?How can I initialise a static Map?Why can't I define a static method in a Java interface?Does Java support default parameter values?Initialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyCreate the perfect JPA entity

Filling region bounded by multiple paths

Why is Colorado so different politically from nearby states?

Plot Taylor–Couette flow ilustratation plot

Is there any Biblical Basis for 400 years of silence between Old and New Testament?

How can I offer a test ride while selling a bike?

How did rebel fighters get past the Scarif shield?

If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?

PhD student with mental health issues and bad performance

How do I get a cleat that's stuck in a pedal, detached from the shoe, out?

Replace value with the value of nearest neighbor in Pandas dataframe

What does symbols in google maps (when looking for some location in uk) mean?

Opposite of "Squeaky wheel gets the grease"

Are there mythical creatures in the world of Game of Thrones?

When to clean out old bird boxes?

Was the 1959 Tibetan Uprising really an uprising?

Do I add my ability modifier to the damage of the bonus-action attack granted by the Crossbow Expert feat?

Comma Code - Ch. 4 Automate the Boring Stuff

What if you don't bring your credit card or debit for incidentals?

Is there a rule that prohibits us from using 2 possessives in a row?

Is there a way to save this session?

Can The Malloreon be read without first reading The Belgariad?

Short story written from alien perspective with this line: "It's too bright to look at, so they don't"

Is it legal in the UK for politicians to lie to the public for political gain?

How to check whether existing code base can deadlock



Not able initialise ArrayList inside default constructor


Create ArrayList from arrayHow do I call one constructor from another in Java?Fastest way to determine if an integer's square root is an integerWhen to use LinkedList over ArrayList in Java?How can I initialise a static Map?Why can't I define a static method in a Java interface?Does Java support default parameter values?Initialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyCreate the perfect JPA entity






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want to declare ArrayList inside my default constructor.
But when I'm doing it, it gives me some problem of reference.



private List<List<Integer>> matrix;

Matrix(List<List<Integer>> matrix)
this.matrix = matrix;


Matrix()
this.matrix = new ArrayList<>();



I guess it's changing the other instances of that class.



Matrix resultedMatrix = new Matrix();


 List<List<Integer>> resultedMatrix = new ArrayList<>();
resultedMatrix.add(Arrays.asList(2, 4, 6));
resultedMatrix.add(Arrays.asList(2, 4, 6));
resultedMatrix.add(Arrays.asList(2, 4, 6));

Matrix expected = new Matrix(resultedMatrix);


org.opentest4j.AssertionFailedError:



Expected :com.bootcamp.matrix.Matrix@32cffa6a



Actual :com.bootcamp.matrix.Matrix@792a0fb3










share|improve this question

















  • 4





    It's not clear what your problem is.

    – Eran
    Mar 24 at 11:41











  • Probably there is other key code involved, code that you're not showing us

    – Hovercraft Full Of Eels
    Mar 24 at 11:43











  • Please consider reading How to Ask to help us help you

    – moneydhaze
    Mar 24 at 11:46











  • Did you expect resultedMatrix == expected to be true? Or perhaps resultedMatrix.equals(expected) to be true? The former cannot be true. The latter can be true if you override equals of your Matrix class.

    – Eran
    Mar 24 at 11:47











  • Where do you get the org.opentest4j.AssertionFailedError?

    – Dorian Gray
    Mar 24 at 11:47

















0















I want to declare ArrayList inside my default constructor.
But when I'm doing it, it gives me some problem of reference.



private List<List<Integer>> matrix;

Matrix(List<List<Integer>> matrix)
this.matrix = matrix;


Matrix()
this.matrix = new ArrayList<>();



I guess it's changing the other instances of that class.



Matrix resultedMatrix = new Matrix();


 List<List<Integer>> resultedMatrix = new ArrayList<>();
resultedMatrix.add(Arrays.asList(2, 4, 6));
resultedMatrix.add(Arrays.asList(2, 4, 6));
resultedMatrix.add(Arrays.asList(2, 4, 6));

Matrix expected = new Matrix(resultedMatrix);


org.opentest4j.AssertionFailedError:



Expected :com.bootcamp.matrix.Matrix@32cffa6a



Actual :com.bootcamp.matrix.Matrix@792a0fb3










share|improve this question

















  • 4





    It's not clear what your problem is.

    – Eran
    Mar 24 at 11:41











  • Probably there is other key code involved, code that you're not showing us

    – Hovercraft Full Of Eels
    Mar 24 at 11:43











  • Please consider reading How to Ask to help us help you

    – moneydhaze
    Mar 24 at 11:46











  • Did you expect resultedMatrix == expected to be true? Or perhaps resultedMatrix.equals(expected) to be true? The former cannot be true. The latter can be true if you override equals of your Matrix class.

    – Eran
    Mar 24 at 11:47











  • Where do you get the org.opentest4j.AssertionFailedError?

    – Dorian Gray
    Mar 24 at 11:47













0












0








0








I want to declare ArrayList inside my default constructor.
But when I'm doing it, it gives me some problem of reference.



private List<List<Integer>> matrix;

Matrix(List<List<Integer>> matrix)
this.matrix = matrix;


Matrix()
this.matrix = new ArrayList<>();



I guess it's changing the other instances of that class.



Matrix resultedMatrix = new Matrix();


 List<List<Integer>> resultedMatrix = new ArrayList<>();
resultedMatrix.add(Arrays.asList(2, 4, 6));
resultedMatrix.add(Arrays.asList(2, 4, 6));
resultedMatrix.add(Arrays.asList(2, 4, 6));

Matrix expected = new Matrix(resultedMatrix);


org.opentest4j.AssertionFailedError:



Expected :com.bootcamp.matrix.Matrix@32cffa6a



Actual :com.bootcamp.matrix.Matrix@792a0fb3










share|improve this question














I want to declare ArrayList inside my default constructor.
But when I'm doing it, it gives me some problem of reference.



private List<List<Integer>> matrix;

Matrix(List<List<Integer>> matrix)
this.matrix = matrix;


Matrix()
this.matrix = new ArrayList<>();



I guess it's changing the other instances of that class.



Matrix resultedMatrix = new Matrix();


 List<List<Integer>> resultedMatrix = new ArrayList<>();
resultedMatrix.add(Arrays.asList(2, 4, 6));
resultedMatrix.add(Arrays.asList(2, 4, 6));
resultedMatrix.add(Arrays.asList(2, 4, 6));

Matrix expected = new Matrix(resultedMatrix);


org.opentest4j.AssertionFailedError:



Expected :com.bootcamp.matrix.Matrix@32cffa6a



Actual :com.bootcamp.matrix.Matrix@792a0fb3







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 11:39









Chandan KumarChandan Kumar

15129




15129







  • 4





    It's not clear what your problem is.

    – Eran
    Mar 24 at 11:41











  • Probably there is other key code involved, code that you're not showing us

    – Hovercraft Full Of Eels
    Mar 24 at 11:43











  • Please consider reading How to Ask to help us help you

    – moneydhaze
    Mar 24 at 11:46











  • Did you expect resultedMatrix == expected to be true? Or perhaps resultedMatrix.equals(expected) to be true? The former cannot be true. The latter can be true if you override equals of your Matrix class.

    – Eran
    Mar 24 at 11:47











  • Where do you get the org.opentest4j.AssertionFailedError?

    – Dorian Gray
    Mar 24 at 11:47












  • 4





    It's not clear what your problem is.

    – Eran
    Mar 24 at 11:41











  • Probably there is other key code involved, code that you're not showing us

    – Hovercraft Full Of Eels
    Mar 24 at 11:43











  • Please consider reading How to Ask to help us help you

    – moneydhaze
    Mar 24 at 11:46











  • Did you expect resultedMatrix == expected to be true? Or perhaps resultedMatrix.equals(expected) to be true? The former cannot be true. The latter can be true if you override equals of your Matrix class.

    – Eran
    Mar 24 at 11:47











  • Where do you get the org.opentest4j.AssertionFailedError?

    – Dorian Gray
    Mar 24 at 11:47







4




4





It's not clear what your problem is.

– Eran
Mar 24 at 11:41





It's not clear what your problem is.

– Eran
Mar 24 at 11:41













Probably there is other key code involved, code that you're not showing us

– Hovercraft Full Of Eels
Mar 24 at 11:43





Probably there is other key code involved, code that you're not showing us

– Hovercraft Full Of Eels
Mar 24 at 11:43













Please consider reading How to Ask to help us help you

– moneydhaze
Mar 24 at 11:46





Please consider reading How to Ask to help us help you

– moneydhaze
Mar 24 at 11:46













Did you expect resultedMatrix == expected to be true? Or perhaps resultedMatrix.equals(expected) to be true? The former cannot be true. The latter can be true if you override equals of your Matrix class.

– Eran
Mar 24 at 11:47





Did you expect resultedMatrix == expected to be true? Or perhaps resultedMatrix.equals(expected) to be true? The former cannot be true. The latter can be true if you override equals of your Matrix class.

– Eran
Mar 24 at 11:47













Where do you get the org.opentest4j.AssertionFailedError?

– Dorian Gray
Mar 24 at 11:47





Where do you get the org.opentest4j.AssertionFailedError?

– Dorian Gray
Mar 24 at 11:47












1 Answer
1






active

oldest

votes


















0














In Java, the non-primitive variables are saved in the memory and the variables are only reference of the objects in the memory. Which means, that each time you are creating a new object the variable referencing it contains only the memory address in which the object is found.
So when you are creating 2 different objects with the same value they are not equal!!



List<List<Integer>> list1 = new ArrayList<>();
List<List<Integer>> list2 = new ArrayList<>();
list1==list2 //returns false


You can implement an equals function in which it will compare the values of the two objects.



list1.equals(list2)





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323395%2fnot-able-initialise-arraylist-inside-default-constructor%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In Java, the non-primitive variables are saved in the memory and the variables are only reference of the objects in the memory. Which means, that each time you are creating a new object the variable referencing it contains only the memory address in which the object is found.
    So when you are creating 2 different objects with the same value they are not equal!!



    List<List<Integer>> list1 = new ArrayList<>();
    List<List<Integer>> list2 = new ArrayList<>();
    list1==list2 //returns false


    You can implement an equals function in which it will compare the values of the two objects.



    list1.equals(list2)





    share|improve this answer





























      0














      In Java, the non-primitive variables are saved in the memory and the variables are only reference of the objects in the memory. Which means, that each time you are creating a new object the variable referencing it contains only the memory address in which the object is found.
      So when you are creating 2 different objects with the same value they are not equal!!



      List<List<Integer>> list1 = new ArrayList<>();
      List<List<Integer>> list2 = new ArrayList<>();
      list1==list2 //returns false


      You can implement an equals function in which it will compare the values of the two objects.



      list1.equals(list2)





      share|improve this answer



























        0












        0








        0







        In Java, the non-primitive variables are saved in the memory and the variables are only reference of the objects in the memory. Which means, that each time you are creating a new object the variable referencing it contains only the memory address in which the object is found.
        So when you are creating 2 different objects with the same value they are not equal!!



        List<List<Integer>> list1 = new ArrayList<>();
        List<List<Integer>> list2 = new ArrayList<>();
        list1==list2 //returns false


        You can implement an equals function in which it will compare the values of the two objects.



        list1.equals(list2)





        share|improve this answer















        In Java, the non-primitive variables are saved in the memory and the variables are only reference of the objects in the memory. Which means, that each time you are creating a new object the variable referencing it contains only the memory address in which the object is found.
        So when you are creating 2 different objects with the same value they are not equal!!



        List<List<Integer>> list1 = new ArrayList<>();
        List<List<Integer>> list2 = new ArrayList<>();
        list1==list2 //returns false


        You can implement an equals function in which it will compare the values of the two objects.



        list1.equals(list2)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 24 at 12:36

























        answered Mar 24 at 11:57









        Nizan IfrachNizan Ifrach

        663




        663





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323395%2fnot-able-initialise-arraylist-inside-default-constructor%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해