【问题标题】:Java comparing two arrays and checking if the First array is consecutive of the SecondJava比较两个数组并检查第一个数组是否与第二个数组连续
【发布时间】:2017-04-03 05:23:55
【问题描述】:

长话短说初学者程序员在这里练习java。现在我正在使用两个数组,我想知道 int arrayA (7,14,21,28) 是否与 int ArrayB 连续,这意味着 arrayB 在数组中是否连续具有 (7,14,21,28)。如果是布尔值,则返回 true,否则返回 false。这是我的代码示例。

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


     }


  }

}
}

【问题讨论】:

  • 这里的“连续”是什么意思?
  • 请注意在我的第一个整数“arrayA”中它的值是 7,14,21,28。第二个数组“arrayB”的值为 1,3,5,7,14,21,28,32。请注意在 arrayB 中它实际上连续具有值 7,14,21,28 这将导致一个真正的布尔值
  • @RC。我重新打开的理由是 OP 的任务是为此编写简单的算法,而不是一个圆滑的单行。
  • @TimBiegeleisen 说得对,我是真正的初学者 Java 程序员,所以就我目前的知识而言,使用 for 循环或嵌套 for 循环确实是我唯一的选择。

标签: java arrays


【解决方案1】:

您可以将它们转换为字符串并使用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

【讨论】:

    【解决方案2】:

    如果您想使用作为 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));
    }
    

    【讨论】:

      【解决方案3】:
           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;
      
      }
      
      }
      

      见上面的例子。

      【讨论】:

      • 请在 result=true; 行之后添加 break;
      • 是否可以在 main 之外添加一个单独的方法来返回布尔值?
      • 是的,你可以这样做。只需在方法中编写上面的代码并从那里返回结果。如果你需要我可以给你代码。
      • 请举个例子,我希望方法命名为 public static boolean 出现Consecutive之类的,非常感谢
      • 欢迎您..总是乐于提供帮助:)
      【解决方案4】:

      如果有人需要算法答案....(我已经颠倒了 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);
       }
      

      【讨论】:

        猜你喜欢
        • 2016-02-21
        • 1970-01-01
        • 2021-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 2016-12-19
        相关资源
        最近更新 更多