如何使用对象打印Java中的一系列行';s属性?
基本上,我有一个学生对象存储在数组列表中,具有以下属性:如何使用对象打印Java中的一系列行';s属性?,java,Java,基本上,我有一个学生对象存储在数组列表中,具有以下属性: -firstName: String -lastName: String -studentID: String -row: int -column: int 现在我想在控制台中以如下方式打印它们: 第一排 学生1 |学生2 |学生4 firstName1 | firstName2 | firstNamen lastName1 | lastName2 | lastNamen 第二排 学生1 |学生2 |学生4 firstName1 | f
-firstName: String
-lastName: String
-studentID: String
-row: int
-column: int
现在我想在控制台中以如下方式打印它们:
第一排
学生1 |学生2 |学生4
firstName1 | firstName2 | firstNamen
lastName1 | lastName2 | lastNamen
第二排
学生1 |学生2 |学生4
firstName1 | firstName2 | firstNamen
lastName1 | lastName2 | lastNamen
属性来自数据库并存储在学生对象中。
它们的存储方式是根据列和行号进行排列。现在,我想以这样的方式打印上面的内容:当row属性更改时,它将打印另一个“行”学生
编辑:我做了什么
public void displayStudents() {
studentListSize = StudentList.size();
int listIndex = 0; // This variable gives you the size of the list
int columnIndex = 0; // The column index gives you the column you are supposedly traversing
int resetIndex = 0; // This index does what its name does which is essentially to reset a value
int continueIndex = 0; // Index which tells one where one supposedly ended, hence the name 'continue'
int currentRow = seats.get(columnIndex).getStudentRow(); // The currentRow method says which row one is currently traversing
for(listIndex = 0; listIndex < seatSize; listIndex++) {
// if the row attribute taken from the list of seats is not equal to the current row then, that means that the row has changed and
// the following loops execute
if(seats.get(listIndex).getStudentRow() != currentRow) {
// The following methods just display stuff. It's essentially a looped version of the commented out method below
System.out.println();
for(columnIndex = resetIndex; columnIndex <= continueIndex; columnIndex++) {
System.out.printf("|%10s|", seats.get(columnIndex).getFirstName());
}
System.out.println();
for(columnIndex = resetIndex; columnIndex <= continueIndex; columnIndex++) {
System.out.printf("|%10s|", seats.get(columnIndex).getLastName());
}
System.out.println();
// setting the reset index to the column index is done because if you don't do this the for loops below will repeat the displayed things above with a new set of columns
resetIndex = columnIndex;
// this sets the currentRow
currentRow = seats.get(listIndex).getStudentRow();
// Print the IDs
System.out.printf("|%10s|", seats.get(listIndex).getStudentID());
// the assignment below ensures that the all rows are printed (for the first set)
continueIndex = listIndex;
}
// The next lines are workarounds which repeat what the above does. There could have been a better way had there been a functioning GOTO keyword in Java
System.out.println();
for(columnIndex = resetIndex; columnIndex <= continueIndex; columnIndex++) {
System.out.printf("|%10s|", seats.get(columnIndex).getFirstName());
}
System.out.println();
for(columnIndex = resetIndex; columnIndex <= continueIndex; columnIndex++) {
System.out.printf("|%10s|", seats.get(columnIndex).getLastName());
}
System.out.println();
public void displayStudents(){
studentListSize=StudentList.size();
int listIndex=0;//此变量提供列表的大小
int columnIndex=0;//列索引提供了假定要遍历的列
int resetIndex=0;//此索引执行其名称所执行的操作,本质上就是重置值
int continueIndex=0;//该索引告诉一个人应该在哪里结束,因此名为“continue”
int currentRow=seats.get(columnIndex).getStudentRow();//currentRow方法表示当前正在遍历的行
对于(listIndex=0;listIndex 对于(columnIndex=resetIndex;columnIndex注意,输出应该在命令行中什么类型的数据库?您根本没有发布任何代码。请告诉我们您首先尝试了什么。看起来第1行实际上是3行,第2行与第3行完全相同。嗯?!您能澄清一下如何将student对象存储在数组列表中吗属性?我用这些属性创建了一个学生对象,并将它们存储在一个列表中。