Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最长公共子序列算法中的界异常_Java_Arraylist_Lcs - Fatal编程技术网

Java 最长公共子序列算法中的界异常

Java 最长公共子序列算法中的界异常,java,arraylist,lcs,Java,Arraylist,Lcs,我写了一个算法,可以找到两个字符串的最长公共子序列 以下是“plik1”文件的内容: 1 11 23 1 18 9 15 23 5 11 1 18 1 20 5 11 1 下面是应该保存在文件2中的内容: 11 1 18 5 编译后出现错误: String 1 : [1, 11, 23, 1, 18, 9, 15, 23, 5] String 2 : [11, 1, 18, 1, 20, 5, 11, 1] 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1

我写了一个算法,可以找到两个字符串的最长公共子序列

以下是“plik1”文件的内容:

1 11 23 1 18 9 15 23 5
11 1 18 1 20 5 11 1
下面是应该保存在文件2中的内容:

11 1 18 5
编译后出现错误:

String 1 : [1, 11, 23, 1, 18, 9, 15, 23, 5]
String 2 : [11, 1, 18, 1, 20, 5, 11, 1]

0 0 0 0 0 0 0 0 0 0 
0 0 1 1 1 1 1 1 1 1 
0 1 1 1 2 2 2 2 2 2 
0 1 1 1 2 3 3 3 3 3 
0 1 1 1 2 3 3 3 3 3 
0 1 1 1 2 3 3 3 3 3 
0 1 1 1 2 3 3 3 3 4 
0 1 2 2 2 3 3 3 3 4 
0 1 2 2 3 3 3 3 3 4 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Zadanie1.main(Zadanie1.java:74)
我不知道,代码有什么问题

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Zadanie1 { 
    public static void main(String[] args) throws Exception {   

    Scanner file1 = new Scanner( new File("plik1.txt") );

    ArrayList<Integer> line1 = new ArrayList<Integer>();
    ArrayList<Integer> line2 = new ArrayList<Integer>();

    Scanner scan = new Scanner(file1.nextLine());
    while (scan.hasNext()){
        line1.add(scan.nextInt());
    }

    scan = new Scanner(file1.nextLine());
    while (scan.hasNext()){
        line2.add(scan.nextInt());
    }

    System.out.println("String 1 : " + line1);
    System.out.println("String 2 : " + line2);

     int[][] table = new int[line1.size()+1][line2.size()+1];

        StringBuffer subsequence = new StringBuffer();

            for (int i = 0; i<=line1.size(); i++) 
                table[i][0] = 0;

            for(int i = 0; i<=line2.size(); i++)
                table[0][i] = 0;

        for(int i = 1; i<=line1.size(); i++) {
            for (int j = 1; j<=line2.size(); j++) {

                if ( line1.get(i-1) == line2.get(j-1) )
                    table[i][j] = table[i-1][j-1] + 1;                  

                else
                    table[i][j] = Math.max(table[i-1][j], table[i][j-1]);

            }
        }

        for(int i=0; i<=line2.size(); i++) {
            System.out.println();
            for(int j=0; j<=line1.size(); j++) {
                System.out.print(table[j][i] + " ");
            }
        }


        for(int x = line1.size()+1, y = line2.size()+1, l1 = line1.size()-1, l2 = line2.size()-1; x != 0 && y != 0;
                l1--, l2--) {

            if( line1.get(l1) != line2.get(l2) ) {
                if( table[x][y-1] > table[x-1][y] ) y--;
                else if( table[x-1][y] > table[x][y-1]) x--;
            }

            else if( line1.get(l1) == line2.get(l2) ) {
                subsequence.append(line1.get(l1-1));
                x--; y--; 
            }
        }

       String buff = subsequence.reverse().toString();

    System.out.print("PO: " + buff);
    System.out.println();

    PrintWriter file2 = new PrintWriter("plik2.txt");

    file2.println(buff);
    file1.close();
    file2.close();
    }
}
import java.io.*;
导入java.util.ArrayList;
导入java.util.Scanner;
公共类Zadanie1{
公共静态void main(字符串[]args)引发异常{
Scanner file1=新扫描仪(新文件(“plik1.txt”);
ArrayList line1=新的ArrayList();
ArrayList line2=新的ArrayList();
Scanner scan=新扫描仪(file1.nextLine());
while(scan.hasNext()){
第1行。添加(scan.nextInt());
}
scan=新扫描仪(file1.nextLine());
while(scan.hasNext()){
第2行。添加(scan.nextInt());
}
System.out.println(“字符串1:+line1”);
System.out.println(“字符串2:+line2”);
int[]table=newint[line1.size()+1][line2.size()+1];
StringBuffer子序列=新的StringBuffer();

对于(inti=0;i,根据问题的内容,我不确定NPE实际发生在哪里,但您创建了一个数组,如

 int[][] table = new int[line1.size()+1][line2.size()+1];
然后您尝试访问:

int x = line1.size()+1
table[x][y-1]

看起来很可疑

我稍微更改了代码:

    for(int x = line1.size()+1, y = line2.size()+1, l1 = line1.size()-1, l2 = line2.size()-1; x != 0 && y != 0;
            l1--, l2--) {

        if( line1.get(l1) != line2.get(l2) ) {
            if( table[x-1][y-2] > table[x-2][y-1] ) y--;
            else if( table[x-2][y-1] > table[x-1][y-2]) x--;
        }

        else if( line1.get(l1) == line2.get(l2) ) {
            subsequence.append(line1.get(l1-1));
            x--; y--; 
        }
    }

但仍然存在一个问题。我知道在哪里,当函数调用时,例如表[x-2][y-1],x是1,它的异常范围。我知道我必须在某个地方添加一个条件,但在哪里和哪个…我试图添加“如果((y-2你是对的:)我更改了它,但问题仍然发生。请看下一篇文章:)关于LCS算法,请参考我的回答:关于LCS算法
String 1 : [1, 11, 23, 1, 18, 9, 15, 23, 5]
String 2 : [11, 1, 18, 1, 20, 5, 11, 1]


0 0 0 0 0 0 0 0 0 0 
0 0 1 1 1 1 1 1 1 1 
0 1 1 1 2 2 2 2 2 2 
0 1 1 1 2 3 3 3 3 3 
0 1 1 1 2 3 3 3 3 3 
0 1 1 1 2 3 3 3 3 3 
0 1 1 1 2 3 3 3 3 4 
0 1 2 2 2 3 3 3 3 4 
0 1 2 2 3 3 3 3 3 4 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Zadanie1.main(Zadanie1.java:73)
for(int x = line1.size()+1, y = line2.size()+1, l1 = line1.size()-1, l2 = line2.size()-1; x != 1 && y != 1 && **l1 < 0 && l2 < 0;**
                l1--, l2--) {