Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: Compare different size arrays

  1. #1
    Jasper is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Compare different size arrays

    Hello,

    I have this issue. I want to compare different size arrays to check my data i got from a serial line. I've got it working but i doesn't feel like the right way to do it. Here is my code :
    Java Code:
    public void CheckData(byte[] buffer, int length) {
       byte [] KeepAlive = new byte[1024];
       KeepAlive[0] = 0x31; 
       KeepAlive[1] = 0x05;
       KeepAlive[2] = 0x32;
       KeepAlive[3] = 0x05;
       byte [] ACK = new byte [1024];
       ACK[0] = 0x06;
       if (Arrays.equals(buffer, KeepAlive)) {
          try {
          outputStream.write(ACK);
          } catch (IOException ex) {}
       }
    }
    So basicly, i call the method CheckData with a buffer of 1024 bytes (can't be changed). Now i want to compare the first 4 bytes of this buffer with a 4-byte KeepAlive sequence. If equal, then send back ACK.

    The above example does work, but a more professional approach would be welcome :)

    Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Compare different size arrays

    What do you want the results of the compare to be if the arrays are different sizes? What does the Arrays equals() method do when the arrays are different sizes?

    You could use a for loop and compare each element of the two arrays.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Compare different size arrays

    I'm amazed the above works, because the keepAlive array has 1020 entries that are all 0 so, unless the given buffer also only has 0 in the end 1020 bytes it will never give true.

    If you want to compare just the first 4 bytes then keepAlive should be byte[4] and the call would be equals(Arrays.copyOf(buffer, 4), keepAlive).
    Also I would suggest turning keepAlive into a static final class constant, KEEP_ALIVE.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Jasper is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Compare different size arrays

    @ Tolls

    Indeed, when the keep alive package is send. The complete buffer has all 0 in the last 1020 bytes. If not, something is wrong. But as I said, this code works.

    Now, I altered my code and used the copyOf method and still got succes. This feels like nicer way to do it. Thanks!
    Also thanks for the tip for using final class constants.

Similar Threads

  1. Compare two Arrays, checking for duplication
    By Redefine12 in forum New To Java
    Replies: 4
    Last Post: 02-03-2012, 10:57 PM
  2. compare arrays
    By innspiron in forum New To Java
    Replies: 4
    Last Post: 04-16-2010, 11:22 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  4. Compare two arrays for difference
    By aaronfsimons in forum New To Java
    Replies: 2
    Last Post: 05-11-2009, 04:49 PM
  5. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •