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;
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
add a comment |
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
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 expectresultedMatrix == expectedto be true? Or perhapsresultedMatrix.equals(expected)to be true? The former cannot be true. The latter can be true if you overrideequalsof yourMatrixclass.
– Eran
Mar 24 at 11:47
Where do you get theorg.opentest4j.AssertionFailedError?
– Dorian Gray
Mar 24 at 11:47
add a comment |
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
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
java
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 expectresultedMatrix == expectedto be true? Or perhapsresultedMatrix.equals(expected)to be true? The former cannot be true. The latter can be true if you overrideequalsof yourMatrixclass.
– Eran
Mar 24 at 11:47
Where do you get theorg.opentest4j.AssertionFailedError?
– Dorian Gray
Mar 24 at 11:47
add a comment |
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 expectresultedMatrix == expectedto be true? Or perhapsresultedMatrix.equals(expected)to be true? The former cannot be true. The latter can be true if you overrideequalsof yourMatrixclass.
– Eran
Mar 24 at 11:47
Where do you get theorg.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
add a comment |
1 Answer
1
active
oldest
votes
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)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
edited Mar 24 at 12:36
answered Mar 24 at 11:57
Nizan IfrachNizan Ifrach
663
663
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 == expectedto be true? Or perhapsresultedMatrix.equals(expected)to be true? The former cannot be true. The latter can be true if you overrideequalsof yourMatrixclass.– Eran
Mar 24 at 11:47
Where do you get the
org.opentest4j.AssertionFailedError?– Dorian Gray
Mar 24 at 11:47