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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:58:59+00:00 2026-05-26T10:58:59+00:00

I have a method to compare two byte arrays. The code is java-style, and

  • 0

I have a method to compare two byte arrays. The code is java-style, and there are many “if-else”s.

def assertArray(b1: Array[Byte], b2: Array[Byte]) {
  if (b1 == null && b2 == null) return;
  else if (b1 != null && b2 != null) {
    if (b1.length != b2.length) throw new AssertionError("b1.length != b2.length")
    else {
      for (i <- b1.indices) {
        if (b1(i) != b2(i)) throw new AssertionError("b1(%d) != b2(%d)".format(i, i))
      }
    }
  } else {
    throw new AssertionError("b1 is null while b2 is not, vice versa")
  }
}

I have tried as following, but it’s not simplified the code much:

(Option(b1), Option(b2)) match {
    case (Some(b1), Some(b2)) => if ( b1.length == b2.length ) {
       for (i <- b1.indices) {
        if (b1(i) != b2(i)) throw new AssertionError("b1(%d) != b2(%d)".format(i, i))
       }
    } else {
       throw new AssertionError("b1.length != b2.length")
    }
    case (None, None) => _
    case _ => throw new AssertionError("b1 is null while b2 is not, vice versa")
}
  • 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-26T10:59:00+00:00Added an answer on May 26, 2026 at 10:59 am

    Unless you’re doing this as an academic exercise, how about

    java.util.Arrays.equals(b1, b2)
    

    The description:

    Returns true if the two specified arrays of bytes are equal to one
    another. Two arrays are considered equal if both arrays contain the
    same number of elements, and all corresponding pairs of elements in
    the two arrays are equal. In other words, two arrays are equal if they
    contain the same elements in the same order. Also, two array
    references are considered equal if both are null.

    I will admit to this being ‘java style’ 🙂

    Since you’re throwing AssertionErrors, you can remove all of the else’s:

    def assertArray(b1: Array[Byte], b2: Array[Byte]): Unit = {
      if (b1 == b2) return;
    
      if (b1 == null || b2 == null) throw new AssertionError("b1 is null while b2 is not, vice versa")  
    
      if (b1.length != b2.length) throw new AssertionError("b1.length != b2.length")
    
      for (i <- b1.indices) {
        if (b1(i) != b2(i)) throw new AssertionError("b1(%d) != b2(%d)".format(i, i))
      }
    }
    

    If, as I suspect, you’re actually using this within JUnit tests (hence the assertArray), then you can use a trick which I often do, compare the string representations of the arrays:

    def assertArray2(b1: Array[Byte], b2: Array[Byte]): Unit = {
      assertEquals(toString(b1), toString(b2))
    }
    
    def toString(b: Array[Byte]) = if (b == null) "null" else java.util.Arrays.asList(b:_*).toString
    

    which will give you the same outcome (an AssertionError), with where the differences are.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does java have a built-in method to compare precedence of two operators? For example,
I have a List<Foo> , and a compare() method taking two Foo objects and
I have a method to compare two list of objects. The objects are unique
Currently, I have this method to compare two numbers Private Function ETForGreaterThan(ByVal query As
I need to do operations like compare that two number arrays have the same
I want to compare two Java objects without overriding equals method . Since I
I have a method which compares two objects and returns a list of all
I have a method in my Python code that returns a tuple - a
I need to compare two date in javascript. In my code I am calling
I have run into a situation where I need to compare two different lists

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.