Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array.equals doesn't work  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working through the cryptopal challenges and am having a problem with comparing 2 arrays. 


I'm not seeing how result is different from expected.  So, show me why I'm stupid

 
Liutauras Vilda
Marshal
Posts: 4783
330
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look for the class Arrays in Java API, there you'll find a method which could be useful for you.
Try always to use Java API site in order to see what language creators have to offer for you.
 
Liutauras Vilda
Marshal
Posts: 4783
330
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think happened in your initial case?
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already fixed the "byte index = 0" to make it an int.  dang.

One of the things I tried was Array.equals(result, expected), but got compile errors along the lines of "got byte[], expected Object".  Even though this says it's legal.
 
Liutauras Vilda
Marshal
Posts: 4783
330
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:
One of the things I tried was Array.equals(result, expected), but got compile errors along the lines of "got byte[], expected Object".  Even though this says it's legal.

Look for Arrays.equals(..., ...)

I'm getting output:
SUCCESS!
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeez, I can't believe I burned 2 hours not noticing that little 's'.

Thanks!
 
Liutauras Vilda
Marshal
Posts: 4783
330
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Jeez, I can't believe I burned 2 hours not noticing that little 's'.

To be honest I don't know how you were able to call static method equal on Array class, as there isn't such.

Anyway, problem had to go away as I have tried on my machine.
 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:. . . not noticing that little 's'.

Thanks!
There is a convention that utility classes, unlike ordinary classes, are plural. Not Collection but Collections. Not Array but Arrays. There are three classes and one interface in the standard API with those names.q

I know the book is old, but in Object-Oriented Software Construction, Bertrand Meyer fulminates against case‑sensitive coding, saying it is not right that there shou‍ld be a semantic difference between two names in programming because they have a different case of their first letter. (Float vs. float or Double vs. double.) Maybe he shou‍ld have complained about the difference between Array and Arrays, too.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the book is old, but in Object-Oriented Software Construction, Bertrand Meyer fulminates against case‑sensitive coding, saying it is not right that there shou‍ld be a semantic difference between two names in programming because they have a different case of their first letter. (Float vs. float or Double vs. double.) Maybe he shou‍ld have complained about the difference between Array and Arrays, too.


I actually bought and read that book years ago.  But my career ended up mainly writing device drivers and kernel stuff (not just including Linux kernel stuff), and C++ just wasn't a good fit for those environments.  So my Object Oriented programming has been meager at best.

I remember when I read that book I thought it was wrong.   This was before Java was released, but I thought some kind of naming convention would be A Good Thing (tm).

I'm retired now, I do Java for fun.  Beats crossword puzzles.

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All this and nobody is wishing me well on challenge 6?  You're either a heartless bunch of folks, or you didn't follow my original link to the cryptopals challenges....

My hamming algorithm returns 0, need to either point Eclipse to it (ugh, I hate Eclipse), or use print statements (ugh, painful).

Anyone else doing the challenges?

What's sad is I have stuff that I need to do, but these challenges are sucking up all my time.  I'm actually having fun solving these things and learning Java at the same time.

<edit>  My hamming algorithm was working, my test code was borked

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!