Skip to main content
Code Review

Return to Answer

  1. I might have misunderstood something but both of these tests fail:

     @Test
     public void test3() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 3, 4, 5 };
     assertEquals(2, findString(in, find)); // returns -1
     }
     @Test
     public void test5() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 4, 5, 6 };
     assertEquals(3, findString(in, find)); // returns -1
     }
    

I think you should increase ii++ here instead of i:

for(int ii = 1; ii < find.length; i++) {

So, try to use variables which are easer to tell apart from each other.

  1. Anyway, don't reinvent the wheel, there is a library for that! Guava has ana IntsBytes class with the following method:
public static int indexOf(byte[] array, byte[] target)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

It's open-source, so you can compare their code with yours.

See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)

  1. I might have misunderstood something but both of these tests fail:

     @Test
     public void test3() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 3, 4, 5 };
     assertEquals(2, findString(in, find)); // returns -1
     }
     @Test
     public void test5() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 4, 5, 6 };
     assertEquals(3, findString(in, find)); // returns -1
     }
    

I think you should increase ii++ here instead of i:

for(int ii = 1; ii < find.length; i++) {

So, try to use variables which are easer to tell apart from each other.

  1. Anyway, don't reinvent the wheel, there is a library for that! Guava has an Ints class with the following method:
public static int indexOf(byte[] array, byte[] target)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

It's open-source, so you can compare their code with yours.

See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)

  1. I might have misunderstood something but both of these tests fail:

     @Test
     public void test3() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 3, 4, 5 };
     assertEquals(2, findString(in, find)); // returns -1
     }
     @Test
     public void test5() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 4, 5, 6 };
     assertEquals(3, findString(in, find)); // returns -1
     }
    

I think you should increase ii++ here instead of i:

for(int ii = 1; ii < find.length; i++) {

So, try to use variables which are easer to tell apart from each other.

  1. Anyway, don't reinvent the wheel, there is a library for that! Guava has a Bytes class with the following method:
public static int indexOf(byte[] array, byte[] target)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

It's open-source, so you can compare their code with yours.

See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)

added 301 characters in body
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157
  1. I might have misunderstood something but both of these tests fail:

     @Test
     public void test3() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 3, 4, 5 };
     assertEquals(2, findString(in, find)); // returns -1
     }
     @Test
     public void test5() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 4, 5, 6 };
     assertEquals(3, findString(in, find)); // returns -1
     }
    

I think you should increase ii++ here instead of i:

for(int ii = 1; ii < find.length; i++) {

So, try to use variables which are easer to tell apart from each other.

  1. Anyway, don't reinvent the weel, there is a library for that! Guava has an Ints class with the following method:

    Anyway, don't reinvent the wheel, there is a library for that! Guava has an Ints class with the following method:
public static int indexOf(byte[] array, byte[] target)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

It's open-source, so you can compare their code with yours.

See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)

  1. I might have misunderstood something but both of these tests fail:

     @Test
     public void test3() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 3, 4, 5 };
     assertEquals(2, findString(in, find)); // returns -1
     }
     @Test
     public void test5() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 4, 5, 6 };
     assertEquals(3, findString(in, find)); // returns -1
     }
    
  2. Anyway, don't reinvent the weel, there is a library for that! Guava has an Ints class with the following method:

public static int indexOf(byte[] array, byte[] target)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)

  1. I might have misunderstood something but both of these tests fail:

     @Test
     public void test3() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 3, 4, 5 };
     assertEquals(2, findString(in, find)); // returns -1
     }
     @Test
     public void test5() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 4, 5, 6 };
     assertEquals(3, findString(in, find)); // returns -1
     }
    

I think you should increase ii++ here instead of i:

for(int ii = 1; ii < find.length; i++) {

So, try to use variables which are easer to tell apart from each other.

  1. Anyway, don't reinvent the wheel, there is a library for that! Guava has an Ints class with the following method:
public static int indexOf(byte[] array, byte[] target)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

It's open-source, so you can compare their code with yours.

See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)

Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157
  1. I might have misunderstood something but both of these tests fail:

     @Test
     public void test3() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 3, 4, 5 };
     assertEquals(2, findString(in, find)); // returns -1
     }
     @Test
     public void test5() {
     byte[] in = { 1, 2, 3, 4, 5, 6 };
     byte[] find = { 4, 5, 6 };
     assertEquals(3, findString(in, find)); // returns -1
     }
    
  2. Anyway, don't reinvent the weel, there is a library for that! Guava has an Ints class with the following method:

public static int indexOf(byte[] array, byte[] target)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)

lang-java

AltStyle によって変換されたページ (->オリジナル) /