2017-04-03 127 views
1

长篇小故事短初学者程序员在这里练习用java。现在我需要两个数组,并且我想知道int arrayA(7,14,21,28)是否与int ArrayB连续,意味着arrayB在数组中连续存在(7,14,21,28)。如果是布尔型,则返回true,否则返回false。这是我的代码的一个例子。Java比较两个数组并检查第一个数组是否与第二个数组连续

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

你是什么意思“连续”在这里? –

+0

注意如何在我的第一个整数“arrayA”中它具有值7,14,21,28。第二个数组“arrayB”的值为1,3,5,7,14,21,28,32。请注意,在arrayB中它确实具有连续值7,14,21,28,这将导致真正的布尔值 –

+0

相关:http://stackoverflow.com/questions/10894197/recursive-function-to-check-是否 - 子阵列 – 2017-04-03 05:32:06

回答

3

你可以将它们转换为字符串,并str.contains()方法的使用

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

1

如果你想使用的方法将你会在生产中可能使用作为一个Java工程师,然后检出@RC给出的重复链接。或@Raman的答案。对于你的任务而言,如果你只想用一个循环来回答问题,那就考虑我的答案。您可以在arrayB上迭代一次,并检查arrayA中包含的数字序列是否发生,而不会中断。

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

更新一:

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; 

} 

} 

见上面的例子。

+1

请加** break; ** ** line result = true; ** –

+0

是否有可能在main之外添加一个单独的方法,还返回布尔值? –

+1

是的,你可以做到这一点。只需在方法中写上面的代码并从那里返回结果。如果你需要,我可以给你代码。 –

0

如果有人需要一个算法答案....(我颠倒了arrayA和arrayB赋值)。

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); 
} 
相关问题