我想從2個文本文件中獲取字符串內容並與表執行聯合。Java關係代數聯盟
但是,我的代碼不斷輸出table @ hexidecimal對象的位置,而不是文件內的字符串內容。
這裏是我的代碼示例:
/**
* -----------------------------------------------------------------------------------------------------
* 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);
}
}
OUTPUT:
//T1
1 A
26 Z
3 C
//T2
1 A
2 B
3 C
//From Set<Table> tableUnion
Union list: [[email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]
「合併」變量聲明在哪裏? – izilotti
如果您將'Table'對象放入您的'Set'中,並在打印時獲得'Table @ 213423',則需要在'Table'中實現'toString()'方法並定義該表的內容應該如何被轉換成一個字符串 – GameDroids
對不起合併被刪除,我只是更新它。感謝您的輸入,Gamedroids,我對toString的想法是一樣的,但將它應用到我的最後一個System.out.println語句中。我應該把toString放在表類中? –