【问题标题】:fill a 2d array with chars of 2 string用 2 个字符串的字符填充 2d 数组
【发布时间】:2013-12-14 16:01:31
【问题描述】:

我的主要目的是创造类似的东西

我目前从我的 txt 文件中读取 2 个 dna 序列并创建 2 个具有 2 个序列(M 和 N)的字符串,因此我必须创建一个 M+1 和 N+1 矩阵才能执行我的动态规划算法。

现在我的问题是这个!

如何创建这个二维数组?我的第一个维度应该用我的第一个字符串(part1)的字符创建,第二个维度应该用我的第二个字符串(part2)创建

我怎样才能做到这一点,然后像图片中一样打印出来。

谢谢 http://i.stack.imgur.com/ViHc9.png

这是我的代码

import java.io.*;
import java.util.Arrays;

import java.util.Scanner;

public class EditDistance {

    public static void main(String[] args) {
        int N = 0;
        int M = 0;
        // char [][] opt = new char [N+1][M+1];

        java.io.File file = new java.io.File("gene57.txt");
        try {
            Scanner input = new Scanner(file);
            while (input.hasNext()) {
                String num = input.nextLine();

                String[] parts = num.split(" ");
                String part1 = parts[0];
                N = part1.length();

                String part2 = parts[1];
                M = part2.length();

                System.out.println(part1);
                System.out.println("Number of nucleobase of Sequence 1 is=" + N);

                System.out.println(part2);
                System.out.println("Number of nucleobase of Sequence 2 is=" + M);
            }
        }

        catch (FileNotFoundException e) {
            System.err.format("File does not exist\n");
        }
        // x= n+1 , y=m+
    }
}

【问题讨论】:

  • 您是否明确需要二维数组,或者是否足以打印图片中给出的矩阵?
  • 好吧,我将使用动态编程和回溯方法来获得“最佳”对齐,所以我认为我需要一个二维数组,这就像我的最终输出 % java EditDistance
  • 您不能使用ArrayList<ArrayList<Character>> 来完成这项工作吗?
  • @splrs 我认为对于动态编程,数组效果最好,因为您可以轻松访问例如上面/后面两行的东西。
  • @user2560965 你需要每一行每一列的额外字段吗?

标签: java arrays dynamic-programming multidimensional-array


【解决方案1】:

我使用示例输入 "ATGA AGCT" 编写了这个小程序(这对于 DNA 来说可能甚至是不可能的,但无论如何)。它设置了一个名为opt 的二维char 数组,您可以在其中始终使用0 访问当前行或列字母。

因此,如果您需要第 4 行中的字母,请使用 opt[4][0]

在 java 中,数组以 0 而不是 1 开头,但由于零始终是字母,您只需输入行号即可获得正确的值。

    int N = 0;
    int M = 0;

    String part1 = null;
    String part2 = null;

    java.io.File file = new java.io.File("gene57.txt");
    try {
        Scanner input = new Scanner(file);
        while (input.hasNext()) {
            String num = input.nextLine();

            String[] parts = num.split(" ");
            part1 = parts[0];
            N = part1.length();

            part2 = parts[1];
            M = part2.length();

            System.out.println(part1);
            System.out.println("Number of nucleobase of Sequence 1 is=" + N);

            System.out.println(part2);
            System.out.println("Number of nucleobase of Sequence 2 is=" + M);
        }
    }

    catch (FileNotFoundException e) {
        System.err.format("File does not exist\n");
    }

    char [][] opt = new char [N + 2][M + 2]; // + 1 for the indicator row/column + 1 for the extra field

    opt[0] = part1.toCharArray();

    char[] temp = part2.toCharArray();

    for (int count = 1; count < M + 1; count++) {
        opt[count][0] =  temp[count - 1];
    }

    // Add '-' for the extra row at the bottom.
    opt[M + 1][0] = '-';        


    // Here you need to do your dynamic programming using op[][] and once you're done
    // you should have a 2d array of integers we'll just call it result[][] for now.

    // replace with the method that computes and returns result! We'll just use these values for now.
    int[][] result = {{1, 2, 3, 4, 5}, {5, 6, 7, 8, 9}, {9, 10, 11, 12, 13}, {13, 14, 15, 16, 17}, {17, 18, 19, 20, 21}};        

    // Printing the matrix

    StringBuilder firstLine = new StringBuilder("      |"); 
    StringBuilder secondLine = new StringBuilder("   x/y|");
    StringBuilder horizontalLine = new StringBuilder("________");

    int count = 0;

    for (count = 0; count < N; count++) {
        if (count < 9) {
            // 1 digit
            firstLine.append("  " + count);
            secondLine.append("  " + opt[0][count]);
            horizontalLine.append("___");
        } else {
            // 2 digits
            firstLine.append(" " + count);
            secondLine.append(" " + opt[0][count]);
            horizontalLine.append("___");
        }
    }

    // Add the extra column at the end for '-'.
    if (count > 9) {
        firstLine.append(" " + count);
        secondLine.append(" -");
        horizontalLine.append("___");
    } else {
        firstLine.append("  " + count);
        secondLine.append("  -");
        horizontalLine.append("___");
    }

    System.out.println(firstLine.toString());
    System.out.println(secondLine.toString());
    System.out.println(horizontalLine.toString());

    for (count = 0; count < M + 1; count++) {

        StringBuilder line = new StringBuilder();

        // Add the indicator stuff
        if (count > 9) {
            line.append(" " + count + " " + opt[count + 1][0] + " |");
        } else {
            line.append("  " + count + " " + opt[count + 1][0] + " |");
        }

        for (int index = 0; index < N + 1; index++) {

            // Add the results
            if (result[count][index] > 9) {
                line.append(" " + result[count][index]);
            } else {
                line.append("  " + result[count][index]);
            }
        }

        // Print the line
        System.out.println(line.toString());
    }

这将打印以下输出:

    ATGA
    Number of nucleobase of Sequence 1 is=4
    AGCT
    Number of nucleobase of Sequence 2 is=4
          |  0  1  2  3  4
       x/y|  A  T  G  A  -
    _______________________
      0 A |  1  2  3  4  5
      1 G |  5  6  7  8  9
      2 C |  9 10 11 12 13
      3 T | 13 14 15 16 17
      4 - | 17 18 19 20 21

如果您对算法有任何疑问,请在下面的 cmets 中告诉我。如果您的 result 值超过 99(达到 3 位),您将不得不调整 if-else 结构并腾出更多空间以保持所有内容正确对齐。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2017-02-02
    相关资源
    最近更新 更多