Java 循环中的 boolean 条件

标签 java arrays loops boolean

我正在尝试计算并返回两个长度相等的 DNA 序列不同的地方。例如,给定字符串“ATGT”和“GTGA”,结果应为数组 { true, false, false, true }。我收到错误/true 无法解析为变量的错误,这是我到目前为止得到的

/**
   * Calculates and returns where two DNA sequences of equal lengths differ. For
   * example, given strings "ATGT" and "GTGA", the result should be array
   * { true, false, false, true }.
   * 
   * @param dna1 a String representing a DNA sequence of arbitrary length (equal
   *          to dna2's length), containing only the characters A, C, G and T
   * @param dna2 a String representing a DNA sequence of arbitrary length (equal
   *          to dna1's length), containing only the characters A, C, G and T
   * @return an array of boolean values, of length equivalent to both
   *         parameters' lengths, containing true in each subscript where the
   *         parameter strings differ, and false where they do not differ
   */
  public static boolean[] mutationPoints(String dna1, String dna2) {
      boolean [] mutPoint =  new boolean [dna1.length()]; 
      for( int i = 0; i < i; i++) {
          if( dna1 != dna2) {
              mutPoint[i] = False; 
          }
          else if (dna1 == dna2) {
              mutPoint[i] = True; 
          }
      }

最佳答案

您需要迭代 mutPoint.length 次(而不是在循环中迭代 i 次)。您想要比较 i 索引处的字符(而不是 String)。并且您需要返回数组。比如,

boolean[] mutPoint = new boolean[dna1.length()];
for (int i = 0; i < mutPoint.length; i++) {
    mutPoint[i] = dna1.charAt(i) != dna2.charAt(i);
}
return mutPoint;

喜欢

char[] dna1arr = dna1.toCharArray();
char[] dna2arr = dna2.toCharArray();
boolean[] mutPoint = new boolean[dna1arr.length];
for (int i = 0; i < mutPoint.length; i++) {
    mutPoint[i] = dna1arr[i] != dna2arr[i];
}
return mutPoint;

关于Java 循环中的 boolean 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33161517/

相关文章:

php - array_push 在 foreach 循环中不起作用

java - GridLayout 上的动画 Sprite

java - android 中的自动旋转器选择

java - 有没有一种方法可以使用 Jersey Clinet 从客户端调用 REST 服务上的 HTTP PATCH

php - Laravel 中如何访问数组

c - 在 c 中将用户输入扫描到 .txt 文件中

java - 如何使用套接字进行线程以便程序不会停止

JavaScript。在 setTimeout 函数中分配错误的值

javascript - jquery更新嵌套对象的键

python - While 在服务器和客户端之间循环 (Python)