fill a 2d array with chars of 2 string

2019-07-23 17:52发布

问题:

My main purpose is to create something like that

I currently read 2 dna sequence from my txt file and create 2 strings with 2 sequence (M and N) so I have to create a M+1 and N+1 matrix in order to perform my dynamic programming algorithm.

now my problem is this!

how can i create this 2d array? My first dimension should created with chars of my first string (part1) and second dimension with my second string (part2)

How can i accomplish that and later on print it like in the picture.

thank you http://i.stack.imgur.com/ViHc9.png

this is my code

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+
    }
}

回答1:

I wrote this little program using the sample input "ATGA AGCT"(which is probably not even possible for DNA but whatever). It sets up a 2d char array called opt where you can always access the current row or column letter with 0.

So if you need the letter in the 4. row you use opt[4][0] for example.

In java arrays start with 0 instead of 1 but since the zero is always the letter you can simply put the row number to get the correct value.

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

This prints the following output:

    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

In case you have any questions about the algorithm let me know in the comments below. If your values for result exceed 99 (going to 3-digits) you would have to adjust the if-else constructs and make more room to keep everything aligned properly.