为什么我的代码在使用文本文件时为什么会打印空数组([])?

问题描述 投票:2回答:1

当我以此方式定义数组字符串时,“ String [] X = {” X“,” M“,” J“,” Y“,” A“,” U“,” Z“}},Y = { “ M”,“ Z”,“ J”,“ A”,“ W”,“ X”,“ U”}“我的代码有效,并且它打印最长的” [M,J,A,U]“ X和Y的常见子序列,但是当我为具有相同输入的字符串数组定义文本文件时,我的代码将打印一个空数组“ []”。我该如何解决?

public class LCS   {
// Function to find LCS of String X[0..m-1] and Y[0..n-1]
public static String A(String[] x, String[] y, int m, int n, int[][] T)
{
    // return empty string if we have reached the end of
    // either sequence
    if (m == 0 || n == 0) {
        return new String();
    }
    // if last character of X and Y matches
    if (x[m - 1] == y[n - 1])
    {
        // append current character (X[m-1] or Y[n-1]) to LCS of
        // substring X[0..m-2] and Y[0..n-2]
        return A(x, y, m - 1, n - 1, T) + x[m - 1];
    }

    // else when the last character of X and Y are different

    // if top cell of current cell has more value than the left
    // cell, then drop current character of String X and find LCS
    // of substring X[0..m-2], Y[0..n-1]

    if (T[m - 1][n] > T[m][n - 1]) {
        return A(x, y, m - 1, n, T);
    }
    else {
        // if left cell of current cell has more value than the top
        // cell, then drop current character of String Y and find LCS
        // of substring X[0..m-1], Y[0..n-2]

        return A(x, y, m, n - 1, T);
    }
}

// Function to fill lookup table by finding the length of LCS
// of substring X[0..m-1] and Y[0..n-1]
public static void LCSLength(String[] x, String[] y, int m, int n, int[][] T)
{
    // fill the lookup table in bottom-up manner
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {

            // if current character of X and Y matches
            if (x[i - 1] == y[j - 1]) {
                T[i][j] = T[i - 1][j - 1] + 1;
            }

            // else if current character of X and Y don't match
            else {
                T[i][j] = Integer.max(T[i - 1][j], T[i][j - 1]);
            }
        }
    }
}

// main function
public static void main(String[] args) throws IOException
{
     String[] X =  read("C:\\Users\\fener\\Desktop\\producerconsumer\\Yeni Metin Belgesi.txt");
     String[] Y = read("C:\\Users\\fener\\Desktop\\producerconsumer\\Yeni Metin Belgesi (2).txt");

    //String[] X = {"X","M","J","Y","A","U","Z"}, Y = {"M","Z","J","A","W","X","U"};
    int m = X.length, n = Y.length;


    // T[i][j] stores the length of LCS of substring
    // X[0..i-1], Y[0..j-1]
    int[][] T = new int[m + 1][n + 1];

    // fill lookup table
    LCSLength(X, Y, m, n, T);

    String[] arr = A(X, Y, m, n, T).split("");
    // find longest common sequence
    System.out.print(Arrays.toString(arr));
    System.exit(0);
}
private static String[] read(String location) throws IOException {
    BufferedReader reader1 = new BufferedReader(new FileReader(location));
    String line;
    ArrayList<String> lines = new ArrayList<String>();
    while ((line = reader1.readLine()) != null) {
        lines.add(line);
    }
    reader1.close();
    String[] result = new String[lines.size()];
    for(int i=0; i<lines.size(); i++) {
        result[i] = lines.get(i);
    }
    return result;  
}
}
java arrays bufferedreader
1个回答
1
投票
© www.soinside.com 2019 - 2024. All rights reserved.