1

Long story short beginner programer here practicing with java. Right now I'm taking two arrays and I want to find out if int arrayA (7,14,21,28) is consecutive to int ArrayB meaning does arrayB have (7,14,21,28) consecutively within the array. If it is the boolean returns true if not it shall simply return false. Here is an example of my code.

 public class Harrison7aTest
 {
 public static void main(String [] args)
 {
  int[] arrayA = {7,14,21,28};
  int[] arrayB = {1,3,5,7,14,21,28,32};

 boolean result = false;

  for(int A = 0; A < arrayA.length - 1; A++)
  {
     for(int B = 0; B < arrayB.length - 1; B++)
     {


     }


  }

}
}
  • 1
    What do you mean by "consecutive" here? – Tim Biegeleisen Apr 03 '17 at 05:26
  • notice how in my first integer "arrayA" that it has the value 7,14,21,28. The second array "arrayB" has the value 1,3,5,7,14,21,28,32. Notice how in arrayB that it does in fact have the value 7,14,21,28 consecutively this would result in a true boolean – Cody Harrison Apr 03 '17 at 05:28
  • related: http://stackoverflow.com/questions/10894197/recursive-function-to-check-whether-sub-array –  Apr 03 '17 at 05:32
  • @RC. I'm reopening on the grounds that the OP's assignment is to write simple algorithm for doing this, rather than a slick one-liner. – Tim Biegeleisen Apr 03 '17 at 05:33
  • @TimBiegeleisen that is correct, I am truly a beginner Java programmer so using for loops or nested for loops are really my only option with my current knowledge. – Cody Harrison Apr 03 '17 at 05:35
  • @TimBiegeleisen do as you wish but there are "algorithmic answers" in my duplicate –  Apr 03 '17 at 05:36

4 Answers4

3

You can convert them to string and make use of str.contains() method

String strArrA = Arrays.toString(arrayA);
String strArrB = Arrays.toString(arrayB);

//to strip square brackets that's comes with Arrays.toString
//for ex: Arrays.toString(arrayA); returns "[7, 14, 21, 28]"
//we want to convert it to "7, 14, 21, 28"
strArrA = strArrA.substring(1, strArrA.length()-1);

if (strArrB.contains(strArrA)) {
    System.out.println("true");
} else {
    System.out.println("false");
}

DEMO

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
1

If you want to use the method would you would likely use in production as a Java engineer, then checkout the duplicate link given by @RC. or the answer by @Raman. For the purposes of your assignment, if you just want to answer the question using a single loop, then consider my answer. You can iterate once over the arrayB and check that the sequence of numbers contained within arrayA occurs, without interruption.

public static boolean containsConsecutive(int[] a, int[] b) {
    int aIndex = 0;
    for (int i=0; i < arrayB.length; i++) {
        if (aIndex != 0 && arrayB[i] != arrayA[aIndex]) {
            break;
        }
        else if (arrayB[i] == arrayA[aIndex]) {
            ++aIndex;
        }
    }

    return aIndex == arrayA.length;
}

public static void main(String[] args) {
    int[] a = {7,14,21,28,32};
    int[] b = {1,3,5,7,14,21,28,32};
    // true
    System.out.println(containsConsecutive(a, b));
    a = {7,14,21,28,32};
    b = {1,3,5,7,8,9,10,14,21,28,32};
    // false - sequence not in order
    System.out.println(containsConsecutive(a, b));
    a = {7,14,21,28,32,35};
    b = {1,3,5,7,14,21,28,32};
    // false - entire sequence in a not contained within b
    System.out.println(containsConsecutive(a, b));
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1
     int[] arrayA = {7,14,21,28};
     int[] arrayB = {1,3,5,7,14,21,28,32};
     boolean result=false;

     for(int i=0;i<arrayB.length;i++){

         if(arrayA[0] == arrayB[i]){
             for(int j=0;j<arrayA.length;j++){
                 if(arrayA[j] == arrayB[i+j]){
                     result=true;
                 }
                 else{
                     result=false;
                 }
             }
         }
     }
    System.out.println(result);

Updated One:

public class Test {

public static void main(String[] args) {

    int[] arrayA = { 7, 14, 21, 28 };
    int[] arrayB = { 1, 3, 5, 7, 14, 21, 28, 32, 7 };
    boolean output = Test.appearsConsecutive(arrayA,arrayB);
    System.out.println(output);
}

public static boolean appearsConsecutive(int[] arrayA, int[] arrayB) {

    boolean result = false;

    for (int i = 0; i < arrayB.length; i++) {

        if (arrayA[0] == arrayB[i]) {
            for (int j = 0; j < arrayA.length; j++) {
                if (arrayA[j] == arrayB[i + j]) {
                    result = true;
                    break;
                } else {
                    result = false;
                }
            }
        }
    }
    System.out.println(result);
    return result;

}

}

See above example.

0

If anybody needs an algorithmic answer....(I've reversed arrayA and arrayB assignments).

public static void main (String[] args) throws java.lang.Exception
{
    // your code goes here
    int[] arrayB = {7,14,21,28};
    int[] arrayA = {1,3,5,7,14,21,28,32};

    boolean result = false;

    for(int A = 0; A < arrayA.length; A++)
    {
         if( arrayA[A] != arrayB[0]) continue;
         //else ... 
         for(int B = 0; B < arrayB.length; B++)
         {
                    if(arrayA[A] != arrayB[B] || A>=arrayA.length) 
                        break; 
                    if(B+1 == arrayB.length) 
                        {
                            result = true;
                            break;
                        }
                    A++;
         }
        if(result) 
            break;

      }
      System.out.println("Contains :"+ result);
 }
dev8080
  • 3,950
  • 1
  • 12
  • 18