Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is it telling me that the two arrays are not the same?

 
Ahmed Samir Saleh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skill[] Skills is an array of skills defined as an instance variable

public void setSkills(Skill []skills){
this.Skills=new Skill[skills.length];
for(int i=0;i<skills.length;i++){
this.Skills[i]=skills[i];
this.skillCount++;
}

}
public Skill[] getSkills(){
if (this.skillCount==0)
return null;
return this.Skills;
}
The test:
Test(timeout = 1000)
public void testGetSkills() {
instance.setSkills(skills);
assertSame("The returned skill array should be the same", instance.getSkills(), skills);
}
The failure:
java.lang.AssertionError: The returned skill array should be the same expected same:<[Leg.edu.guc.lol.game.champions.Skill;@1a8c4e7> was not:<[Leg.edu.guc.lol.game.champions.Skill;@1172e08>


 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this help ?
 
Ahmed Samir Saleh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i already saw it before posting. i have a problem referring Skills array to the method parameter (input) skill array(i want them to refer to the same thing).(i mean i tried to write this.Skills=skills without the for loop, but it didnt work )
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmed Samir Saleh wrote:i mean i tried to write this.Skills=skills without the for loop, but it didnt work

ItDoesntWorkIsUseless
 
Ahmed Samir Saleh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Ahmed Samir Saleh wrote:i mean i tried to write this.Skills=skills without the for loop, but it didnt work

ItDoesntWorkIsUseless


i made the setSkill method like that:
public void setSkills(Skill []skills){
this.Skills=new Skill[skills.length];
this.Skills=skills;
}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmed Samir Saleh wrote:
Joanne Neal wrote:
Ahmed Samir Saleh wrote:i mean i tried to write this.Skills=skills without the for loop, but it didnt work

ItDoesntWorkIsUseless


i made the setSkill method like that:

Okay. Apart from the creation of a new array on line 2 being redundant, what was the problem with this ?
 
Ahmed Samir Saleh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem is that the assertSame tells me that the two arrays are not equal and i need them to be the same but i dont know how.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Ahmed Samir Saleh wrote:the problem is that the assertSame tells me that the two arrays are not equal and i need them to be the same but i dont know how.

So when you had the setSkills method like this, how did you implement the getSkills method ? Was it the same as in your original post ?
 
Ahmed Samir Saleh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ahmed Samir Saleh wrote:the problem is that the assertSame tells me that the two arrays are not equal and i need them to be the same but i dont know how.

So when you had the setSkills method like this, how did you implement the getSkills method ? Was it the same as in your original post ?


yes, exactly
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well in your setSkills method you no longer alter the value of skillCount, so it would have its default value of 0. What does getSkills return if skillCount is 0 ?
 
Ahmed Samir Saleh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Well in your setSkills method you no longer alter the value of skillCount, so it would have its default value of 0. What does getSkills return if skillCount is 0 ?

yes i know that is another problem but its solvable. i can make a counter for each skill in the Skills array. but the main problem is that i cant make the skills array equals to the Skills array (which is an instance variable) so it wont give me an assertion error
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmed Samir Saleh wrote:
Joanne Neal wrote:Well in your setSkills method you no longer alter the value of skillCount, so it would have its default value of 0. What does getSkills return if skillCount is 0 ?

yes i know that is another problem but its solvable. i can make a counter for each skill in the Skills array. but the main problem is that i cant make the skills array equals to the Skills array (which is an instance variable) so it wont give me an assertion error

But in your assertSame statement you are comparing your skills array with what the getSkills method returns. So I ask you again, what does the getSkills method return if skillCount is 0 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic