Java关系代数联合
我试图从2个文本文件中获取字符串内容,并与表进行联合 然而,我的代码不断输出table@hexidecimal对象位置,而不是文件中的字符串内容 以下是我的代码示例:Java关系代数联合,java,Java,我试图从2个文本文件中获取字符串内容,并与表进行联合 然而,我的代码不断输出table@hexidecimal对象位置,而不是文件中的字符串内容 以下是我的代码示例: /** * ----------------------------------------------------------------------------------------------------- * Purpose : This class is used to create a row for a two
/**
* -----------------------------------------------------------------------------------------------------
* Purpose : This class is used to create a row for a two dimensional data
*
* -----------------------------------------------------------------------------------------------------
*/
class Table {
String ID = "";
String name = "";
Table(String ID, String name) {
this.ID = ID;
this.name = name;
}
}
/**
* -----------------------------------------------------------------------------------------------------
* Purpose : This class is used to show how to work with two dimensional data
* -----------------------------------------------------------------------------------------------------
*/
public class RelationalAlgebra {
/**
* It reads a two columns table into a two dimensional array
*
* @return ArrayList<Table>
* @throws IOException
*/
ArrayList<Table> getTable(String fileName) throws IOException {
ArrayList<Table> T1 = new ArrayList<Table>(); // creates an array list
File inFile = new File(fileName); // creates a file object
Scanner scanner = new Scanner(inFile); // Scanner is a reader class
int repetition = 1; // used to skip the 1st line from input file
while (scanner.hasNext()) { // reads until not data
if (repetition == 1) { // if 1st line, skips
scanner.next();
scanner.next();
repetition = 2;
} else { // else reads each column
String ID = scanner.next();
String name = scanner.next();
T1.add(new Table(ID, name));
}
}
scanner.close(); // close input stream
return T1; // returns the new table in the form of ArrayList
}
/**
* It prints the content of an ArrayList<Table>
*
* @param t
*/
void printTable(ArrayList<Table> t) {
for (int i = 0; i < t.size(); i++) {
System.out.println(t.get(i).ID + "\t" + t.get(i).name);
}
}
/**
* An entry point for program execution
*
* @param args
*/
public static void main(String[] args) throws IOException {
RelationalAlgebra rel = new RelationalAlgebra(); // creates an object of this class
ArrayList<Table> T1 = new ArrayList<Table>(); // creates an instance of ArrayList
ArrayList<Table> T2 = new ArrayList<Table>();
T1 = rel.getTable("C:/Users/workspace/RelationalAlgebra/src/T1.txt");
T2 = rel.getTable("C:/Users/workspace/RelationalAlgebra/src/T2.txt");
rel.printTable(T1);
rel.printTable(T2);// prints the newly created table
//Performs Union without duplicates
Set<Table> tableUnion = new LinkedHashSet<Table>();
tableUnion.addAll(T1);
tableUnion.addAll(T2);
tableUnion.toString();
System.out.println("Union : " + tableUnion);
}
}
/**
* -----------------------------------------------------------------------------------------------------
*用途:此类用于为二维数据创建行
*
* -----------------------------------------------------------------------------------------------------
*/
类表{
字符串ID=“”;
字符串名称=”;
表(字符串ID、字符串名称){
this.ID=ID;
this.name=名称;
}
}
/**
* -----------------------------------------------------------------------------------------------------
*目的:本课程用于演示如何处理二维数据
* -----------------------------------------------------------------------------------------------------
*/
公共类关系代数{
/**
*它将一个两列表读入一个二维数组
*
*@returnarraylist
*@抛出异常
*/
ArrayList getTable(字符串文件名)引发IOException{
ArrayList T1=新建ArrayList();//创建数组列表
File inFile=新文件(文件名);//创建文件对象
Scanner Scanner=new Scanner(infle);//Scanner是一个读卡器类
int repeation=1;//用于跳过输入文件的第1行
while(scanner.hasNext()){//读取数据,直到没有数据为止
if(repeation==1){//if第1行,跳过
scanner.next();
scanner.next();
重复次数=2次;
}else{//else读取每一列
字符串ID=scanner.next();
字符串名称=scanner.next();
T1.增加(新表(ID、名称));
}
}
scanner.close();//关闭输入流
return T1;//以ArrayList的形式返回新表
}
/**
*它打印ArrayList的内容
*
*@param t
*/
无效打印表(ArrayList t){
对于(int i=0;i
输出:
//T1
1 A
26 Z
3 C
//T2
1 A
2 B
3 C
//From Set<Table> tableUnion
Union list: [Table@55f96302, Table@3d4eac69, Table@42a57993, Table@75b84c92, Table@6bc7c054, Table@232204a1]
//T1
1A
26 Z
3 C
//T2
1A
2 B
3 C
//从集合表联合
工会名单:[Table@55f96302, Table@3d4eac69, Table@42a57993, Table@75b84c92, Table@6bc7c054, Table@232204a1]
因此,如果将简单的字符串
放入集合
,Java知道该做什么以及如何打印集合。对于那些不太清楚如何打印内容的类,需要实现toString()
方法。在您的情况下,类似于:
class Table {
String ID = "";
String name = "";
Table(String ID, String name) {
this.ID = ID;
this.name = name;
}
@Override
public String toString(){
return ID+" - "+ name; // you can give the string any form you want and you can print anything you want. All that is required is that you return a String in that method.
}
}
另一个注意事项是:即使ID对于数据库来说很常见,在Java中变量也应该总是以小写字母开头。只有类名以大写字母开头。在哪里声明了“merged”变量?如果将
表
对象放入集合
并得到Table@213423
打印时,需要实现toString()表中的
方法
并在其中定义如何将表的内容转换为一个字符串如果删除了合并的内容,我只是对其进行了更新。感谢您的输入,Gamedroids,我对toString也有同样的想法,但将其应用到了我的上一个System.out.println语句中。我应该把toString放在table类的什么地方?我是Java新手,在大学里学习。因此,任何新手级别的建议都将不胜感激。谢谢非常感谢您的帮助,并增加了java和Gamedroid方面的知识。我会试试看,然后和你一起回去。太好了!成功了!非常感谢。我爱你!:)令人惊叹的很高兴它有帮助!嘿,你能帮我做交叉点、差异点和连接点吗?