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++)
     {


     }


  }

}
}
6
  • 1
    What do you mean by "consecutive" here? Commented Apr 3, 2017 at 5: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 Commented Apr 3, 2017 at 5:28
  • related: stackoverflow.com/questions/10894197/… Commented Apr 3, 2017 at 5: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. Commented Apr 3, 2017 at 5: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. Commented Apr 3, 2017 at 5:35

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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));
}

Comments

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.

9 Comments

Kindly add break; after line result=true;
is it possible to add a separate method out of main that will return the boolean as well?
Yes you can do that. Just write above code in a method and return result from there. If you need I can give code to you.
please provide an example, I would like the method to be named something like public static boolean appearsConsecutive or something like that, thank you very much
Your Welcome..Always Happy to help :)
|
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);
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.