我需要比较两个对象数组以检查一个数组元素与其他数组的所有值。如果我以这种方式执行,它会在 if 语句中显示 NULL 点异常。
package unlcomp;
import java.util.HashMap;
import java.util.List;
public class relation {
int rcount=0;
int r1count=0;
public String[] rel=new String[100];
relation rm[]=new relation[100];
relation rm1[]=new relation[100];
public String[] UW1=new String[1000];
public relation[] hash(String[] s,String[] s1,int rcount) {
for(int i=1;i<=rcount;i++) {
rm[i]=new relation();
rm[i].rel[i]=s[i];
rm[i].UW1[i]=s1[i];
}
return rm;
// System.out.println(rcount);
}
public relation[] hash1(String[] s,String[] s1,int r1count) {
for(int i=1;i<=r1count;i++) {
rm1[i]=new relation();
rm1[i].rel[i]=s[i];
rm1[i].UW1[i]=s1[i];
}
return rm1;
}
public void compare() {
relation r[]= rm;
relation r1[]=rm1;
for(int i=1;i<r.length;i++) {
for(int j=1;j<r1.length;j++) {
if(r1[i].rel[i].equals(r[j].rel[j])) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
}
}
我需要从另一个类名调用这个比较方法。
这是调用上述函数的类..
package unlcomp;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Unlcomp {
public String[]rel;
public String[] UW1;
public String[] UW2=new String[100];
public String[] att1=new String[100];
public String[] att2=new String[100];
int i=0;
public String[] store=new String[500];
String pattern2="[-a-z0-9R:._-`&=*'`~\"\\+[\\s]]+[\\(]";
//String pattern = "[(]+[-a-z0-9R:._-`&=*'`~\"\\+[\\s]]+[\\(.,]";
String pattern = "[(]+[-a-z0-9R:_]+[(]+[-a-z0-9R:_-`&=*'`~&+,:;=?@#'<>.^*%!-\"\\+[\\s]]+[)]+[\\.\\,\\:]";
String patterna = "[(]+[-a-z0-9R:_]+[(]+[-a-z0-9R:_-`&=*'`~&+,:;=?@#'<>.^*%!-\"\\+[\\s]]+[)]+[\\,]";
//String pattern1="[,]+[-a-z0-9R:._-`&=*'`~\"\\+[\\s]]+[\\(.]";
String pattern1="[,]+[-a-z0-9R:_]+[(]+[-a-z0-9R_,>-`&=*'`~&+,:;=?@#'<>.^*%!-\"\\+[\\s]]+[)]+[\\)\\.]";
//String pattern1a="[,]+[-a-z0-9R:_]+[(]+[-a-z0-9R_,>-`&=*'`~&+,:;=?@#'<>.^*%!-\"\\+[\\s]]+[)]+[\\.]";
String pattern3="[\\)]+[\\.@]+[-a-z0-9R:._-`&=*'`~&+,:;=?@#'<>.^*%!-\"\\+[\\s]]+[\\ ,]";
String pattern4="[\\)]+[\\.@]+[-a-z0-9R:._-`&=*'`~&+,:;=?@#'<>.^*%!-\"\\+[\\s]]+[\\ )]";
Pattern r = Pattern.compile(pattern);
//Pattern ra = Pattern.compile(patterna);
Pattern r1 = Pattern.compile(pattern1);
//Pattern r1a = Pattern.compile(pattern1a);
Pattern r2 = Pattern.compile(pattern2);
Pattern r3 = Pattern.compile(pattern3);
Pattern r4 = Pattern.compile(pattern4);
String line;
relation obj=new relation();
private int rcount=0;
public void preprocess(String pathf1,String pathf2) throws Exception {
try {
Scanner scanner = new Scanner(new File(pathf1));
scanner.useDelimiter("###");
Scanner scanner1 = new Scanner(new File(pathf2));
scanner1.useDelimiter("###");
//BufferedReader br1 = new BufferedReader(new FileReader(pathf2));
if( scanner.hasNext()) {
if(scanner1.hasNext())
extract(scanner.next());
obj.hash(rel,UW1,rcount);
extract(scanner1.next());
obj.hash1(rel,UW1,rcount);
obj.compare();
}
scanner.close();
scanner1.close();
} catch (IOException e) {}
}
public void extract(String line) {
String [] lines=line.split("\n");
System.out.println(line);
rel=new String[100];
UW1=new String[100];
for(String line1: lines ) {
// rel=null;
// UW1=null;
//UW2=null;
//att1=null;
//att2=null;
Matcher m2 = r2.matcher(line1);
Matcher m1 = r1.matcher(line1);
Matcher m3 = r3.matcher(line1);
Matcher m4 = r4.matcher(line1);
Matcher m = r.matcher(line1);
if( m2.find()) {
rel[i]=m2.group();
rel[i]=rel[i].substring(0, rel[i].length()-1).trim();
rcount++;
//System.out.println(rel[i]);
}
if(m.find()) {
UW1[i]=m.group();
UW1[i]=UW1[i].substring(1, UW1[i].length()-1).trim();
}
i++;
}
}
public static void main(String[] args) throws Exception {
Unlcomp u=new Unlcomp();
String pathsr="E:/M.E/project/test.txt";
String pathdes="E:/M.E/project/test1.txt";
relation r=new relation();
u.preprocess(pathsr,pathdes);
}
}
这需要其中的值,rm
它需要数组中的对象中的值。我使用system.out.println()
语句检查了它。我不知道如何退货。这是这个概念的完整编码,它从文件中读取输入。