填充2串字符二维数组 [英] fill a 2d array with chars of 2 string
问题描述
我的主要目的是为了创造这样的事情
目前,我从我的txt文件读取2的DNA序列,并创建2个字符串以2序列(M和N),所以我为了履行我的动态规划算法来创建一个M + 1和N + 1矩阵。
现在我的问题是这样的!
我怎么可以创建此二维数组?我的第一个尺寸应与我的第二个字符串我的第一个字符串(第一部分)和第二维的字符创建(分2)
我怎么能做到这一点,后来在照片打印像。
感谢您
http://i.stack.imgur.com/ViHc9.png
这是我的code
进口java.io. *;
进口java.util.Arrays中;进口java.util.Scanner中;公共类EditDistance { 公共静态无效的主要(字串[] args){
INT N = 0;
INT M = 0;
//的char [] []选择=新的char [N + 1] [M + 1]; java.io.File的文件=新的java.io.File(gene57.txt);
尝试{
扫描仪输入=新的扫描仪(文件);
而(input.hasNext()){
字符串NUM = input.nextLine(); 的String [] =零件num.split();
第一部分的String =零件[0];
N = part1.length(); 第二部分字符串部分= [1];
M = part2.length(); 的System.out.println(第一部分);
的System.out.println(序列1的核碱基的数目是=+ N); 的System.out.println(第二部分);
的System.out.println(序列2的核碱基的数目是=+ M);
}
} 赶上(FileNotFoundException异常五){
System.err.format(文件不存在\\ n);
}
// X = N + 1,Y = M +
}
}
我写了使用样本输入ATGA AGCT
(这可能不是甚至有可能这个小程序对于DNA但不管)。它建立了一个2D 字符
阵列名为选择
在这里你可以随时与<$ C访问当前的行或列字母$ C> 0 。
所以如果你需要使用4行中的字母选择[4] [0]
例如
在Java数组开始与 0
而不是 1
但由于零总是信你可以简单地把行号,以获得正确的值。
INT N = 0;
INT M = 0; 第一部分的String = NULL;
第二部分的String = NULL; java.io.File的文件=新的java.io.File(gene57.txt);
尝试{
扫描仪输入=新的扫描仪(文件);
而(input.hasNext()){
字符串NUM = input.nextLine(); 的String [] =零件num.split();
第一部分=零件[0];
N = part1.length(); 第二部分=部分[1];
M = part2.length(); 的System.out.println(第一部分);
的System.out.println(序列1的核碱基的数目是=+ N); 的System.out.println(第二部分);
的System.out.println(序列2的核碱基的数目是=+ M);
}
} 赶上(FileNotFoundException异常五){
System.err.format(文件不存在\\ n);
} 的char [] []选择=新的char [N + 2] [M + 2];对于额外的字段// + 1则表示行/列+ 1 选择[0] = part1.toCharArray(); 的char []临时= part2.toCharArray(); 对于(诠释计数= 1;计数&LT; M + 1;计数++){
选择[计数] [0] =温度[数 - 1];
} //添加 - 号底部的额外的行。
选择[M + 1] [0] =' - ';
//这里需要使用运算[] [],一旦你完成做你的动态规划
//你应该有一个整数的二维数组我们只是把它称为导致[] []现在。 //与计算并返回结果的方法代替!我们只使用这些值现在。
INT [] []结果= {{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}}; //打印矩阵 StringBuilder的firstLine中=新的StringBuilder(|);
StringBuilder的secondLine =新的StringBuilder(X / Y |);
StringBuilder的horizontalLine =新的StringBuilder(________); 诠释计数= 0; 为(计数= 0; COUNT&LT; N;计数++){
如果(计数&LT; 9){
// 1位
firstLine.append(+计数);
secondLine.append(+选择[0] [计数]);
horizontalLine.append(____);
}其他{
// 2位数
firstLine.append(+计数);
secondLine.append(+选择[0] [计数]);
horizontalLine.append(____);
}
} ' - '//在月底添加额外的列。
如果(计数&GT; 9){
firstLine.append(+计数);
secondLine.append( - );
horizontalLine.append(____);
}其他{
firstLine.append(+计数);
secondLine.append( - );
horizontalLine.append(____);
} 的System.out.println(firstLine.toString());
的System.out.println(secondLine.toString());
的System.out.println(horizontalLine.toString()); 为(计数= 0; COUNT&LT; M + 1;计数++){ StringBuilder的行=新的StringBuilder(); //添加指示剂的东西
如果(计数&GT; 9){
line.append(+数++选择[计数+ 1] [0] +|);
}其他{
line.append(+数++选择[计数+ 1] [0] +|);
} 对于(INT指数= 0;指数 - LT; N + 1;指数++){ //添加结果
如果(结果[计数] [指数]&GT; 9){
line.append(+结果[计数] [指数]);
}其他{
line.append(+结果[计数] [指数]);
}
} //打印线
的System.out.println(line.toString());
}
这打印输出如下:
ATGA
序列1的核碱基的数目为4 =
AGCT
序列2的核碱基的数目为4 =
| 0 1 2 3 4
X / Y |一件T G A -
_______________________
0 | 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
如果您有关于算法的任何问题,让我知道在下面的意见。如果你的结果值
超过 99
(将3位数),你将不得不调整的if-else
结构,使更多的空间来让一切正确对齐。
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+
}
}
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.
这篇关于填充2串字符二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!