目录
1.什么是并交差
并 Union 扩大(但不重复)
差 Different 减去元素
交 Intersection 重复元素
2.如何做运算的优化
优化的方法:对于重复元素,后续不需要参与比较,可在复制表中删去该元素。
优化的原理:表中不会出现重复元组。
3.表的储存形式
数据存储形式:txt文本文件
假设每个字段是字符型,用‘ , ’作为数据分隔符
4.需要的表及表的说明
表名 | 说明 |
a | 表a,读取文件得到 |
b | 表b,读取文件得到 |
temp | 临时表 |
copya | 表a的副本,修改时不影响表a |
5.数据表文件实例
文件名:db_1.txt
内容:
sno,sname,sex
20085072,AA,男
20085104,BB,男
20091871,CC,男
20094364,DD,男
文件名:db_2.txt
内容:
sno,sname,sex
20085072,AA,男
20092374,EE,女
20094355,FF,男
20094360,GG,男
20094364,DD,男
测试文件读取路径如下:"C:\\db_1.txt","C:\\db_2.txt"
测试文件输出路径如下:"C:\\db_3.txt","C:\\db_4.txt","C:\\db_5.txt"
6.实现代码
6.1 表类
import java.io.*;
import java.util.*;
public class Table1
{
//属性
//属性数不定
public List<String> name=new ArrayList<String>();//属性名
public List<ArrayList> dt=new ArrayList<ArrayList>();//属性值
public String filename;//文件名
public int count=0;
public Table1(String a) throws Exception
{
filename=a;
if (a.equals("null"))
{
}
else
{
//读文件
File file = new File(a);
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
String t = bf.readLine();
Scanner sc = new Scanner(t).useDelimiter(",");
//读属性名
while (sc.hasNext()) {
name.add(sc.next());
count++;
}
for (int i = 0; i < count; i++)
dt.add(new ArrayList<String>());
//读属性值
t = bf.readLine();
while (t != null) {
sc = new Scanner(t).useDelimiter(",");
int i = 0;
while (sc.hasNext()) {
dt.get(i++).add(sc.next());
}
t = bf.readLine();
}
//close
sc.close();
bf.close();
}
}
protected void finalize() throws Exception
{
save(filename);
}
public void save(String a) throws Exception
{
//写文件
File file=new File(a);
BufferedWriter bf=new BufferedWriter(new FileWriter(file));
String t=null;
//写属性名
t=name.get(0);
for(int i=0;i<count;i++)
t=t+","+name.get(i);
t=t+"\n";
bf.write(t);
//写属性值
int i=0;
for(i=0;i<dt.get(0).size();i++)
{
//写一行
t=(String) dt.get(0).get(i);
for(int j=1;j<count;j++)
t=t+","+dt.get(j).get(i);
t=t+"\n";
bf.write(t);
}
//close
bf.close();
}
//插入元组
//---
public void add(List<ArrayList> dt,int index)
{
for(int i=0;i<count;i++)
this.dt.get(i).add(dt.get(i).get(index));
}
//删除元组
public void del(int index)
{
for(int i=0;i<count;i++)
dt.get(i).remove(index);
}
//比较两表某元组
//---
public boolean judge(Table1 tb,int indexa,int indexb)
{
int c=0;
for(int i=0;i<count;i++)
if(!this.dt.get(i).get(indexa).equals(tb.dt.get(i).get(indexb)))
c=1;
if(c==0) return true;
else return false;
}
//输出表中各元组
//---
public void show()
{
if (count==0)
{
}
else
{
//输出属性名
String t = name.get(0);
for (int i = 0; i < count; i++)
t = t + "," + name.get(i);
t = t + "\n";
System.out.print(t);
//输出属性值
int i = 0;
for (i = 0; i < dt.get(0).size(); i++)
{
//写一行
t = (String) dt.get(0).get(i);
for (int j = 1; j < count; j++)
t = t + "," + dt.get(j).get(i);
t = t + "\n";
System.out.print(t);
}
}
}
//并Union
//---
public Table1 union(Table1 tb) throws Exception
{
//指针
int r,s=0;
//新temp表和copya表
Table1 temp=new Table1("null");
Table1 copya=new Table1("null");
//将this表抄到新表和copya表
copya.count=temp.count=this.count;
for(int i=0;i<count;i++)
{
temp.dt.add(new ArrayList<String>());
copya.dt.add(new ArrayList<String>());
temp.name.add(name.get(i));
copya.name.add(name.get(i));
}
for(r=0;r<this.dt.get(0).size();r++)
{
for(int i=0;i<count;i++)
{
temp.dt.get(i).add(dt.get(i).get(r));
copya.dt.get(i).add(dt.get(i).get(r));
}
}
//将tb表中各元组与copya表中各元组比较
for(s=0;s<this.dt.get(0).size();s++)
{
int judge=-1;
for(r=0;r<copya.dt.get(0).size();r++)
{
//若相同,删掉copya中r元组,不参与以后的比较
if(copya.judge(tb, r, s))
{
judge=0;
copya.del(r);
break;
}
}
//将在tb表中与this表不相同的元组添加到新temp表
if(judge==-1)
{
temp.add(tb.dt,s);
}
}
//返回新表
return temp;
}
//交Different
//---
public Table1 different(Table1 tb) throws Exception
{
//指针
int r,s=0;
//新temp表和copya表
Table1 temp=new Table1("null");
Table1 copya=new Table1("null");
//将this表抄到copya表
copya.count=temp.count=this.count;
for(int i=0;i<count;i++)
{
temp.dt.add(new ArrayList<String>());
copya.dt.add(new ArrayList<String>());
temp.name.add(name.get(i));
copya.name.add(name.get(i));
}
for(r=0;r<this.dt.get(0).size();r++)
{
for(int i=0;i<count;i++)
{
copya.dt.get(i).add(dt.get(i).get(r));
}
}
//将tb表中各元组与copya表中各元组比较
for(s=0;s<tb.dt.get(0).size();s++)
{
int judge=-1;
for(r=0;r<copya.dt.get(0).size();r++)
{
//若相同,删掉copya中r元组,不参与以后的比较
if(copya.judge(tb, r, s))
{
judge=0;
copya.del(r);
break;
}
}
//将在两表中都出现的元组添加到新temp表
if(judge==0)
{
temp.add(tb.dt, s);
}
}
//返回新表
return temp;
}
//差Intersection
//---
public Table1 intersection(Table1 tb) throws Exception
{
//指针
int r,s=0;
//新temp表和copya表
Table1 temp=new Table1("null");
Table1 copya=new Table1("null");
//将this表抄到copya表
copya.count=temp.count=this.count;
for(int i=0;i<count;i++)
{
temp.dt.add(new ArrayList<String>());
copya.dt.add(new ArrayList<String>());
temp.name.add(name.get(i));
copya.name.add(name.get(i));
}
for(r=0;r<this.dt.get(0).size();r++)
{
for(int i=0;i<count;i++)
{
copya.dt.get(i).add(dt.get(i).get(r));
}
}
//将tb表中各元组与copya表中各元组比较
for(s=0;s<tb.dt.get(0).size();s++)
{
for(r=0;r<copya.dt.get(0).size();r++)
{
//若相同,删掉copya中r元组,不参与以后的比较
if(copya.judge(tb, r, s))
{
copya.del(r);
break;
}
}
}
//将copya表剩下的元组抄到新temp表
for(r=0;r<copya.dt.get(0).size();r++)
{
temp.add(copya.dt, r);
}
//返回新表
return temp;
}
}
6.2 测试类
public class Test2
{
public static void main(String[] args) throws Exception
{
//注意路径是否需要修改
Table1 a=new Table1("C:\\db_1.txt"),b=new Table1("C:\\db_2.txt"),s=new Table1("null");
//a表显示
System.out.println();
System.out.println("a表");
System.out.println("-----------------");
a.show();
System.out.println("-----------------");
//b表显示
System.out.println();
System.out.println("b表");
System.out.println("-----------------");
b.show();
System.out.println("-----------------");
//并Union
s=a.union(b);
System.out.println();
System.out.println("s表=a并b");
System.out.println("-----------------");
s.show();
System.out.println("-----------------");
s.save("C:\\db_3.txt");
//交Different
s=a.different(b);
System.out.println();
System.out.println("s表=a交b");
System.out.println("-----------------");
s.show();
System.out.println("-----------------");
s.save("C:\\db_4.txt");
//差Intersection
s=a.intersection(b);
System.out.println();
System.out.println("s表=a差b");
System.out.println("-----------------");
s.show();
System.out.println("-----------------");
s.save("C:\\db_5.txt");
}
}