Я пытаюсь взять содержимое String из 2 текстовых файлов и выполнить объединение с таблицами.Java Relational Algebra Union
Однако мой код продолжает выводить таблицу @ hexidecimal location объекта, а не содержимое строки внутри файла.
Вот пример моего кода:
/**
* -----------------------------------------------------------------------------------------------------
* 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);
}
}
ВЫВОД:
//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' при печати, вам нужно реализовать метод' toString() 'в' Таблица' и определить там, как содержимое таблицы должно преобразуется в одну строку – GameDroids
Извините, что Объединенный удален, я только что обновил его. Спасибо за ваш вклад, Gamedroids, я тоже думал о toString, но применил его к моей последней инструкции System.out.println. Где я должен помещать toString в класс таблицы? –