我需要先将行号按行号排序,然后按列号排序。每当用户set(row, col, x)
时,它都会被放入某个位置的rowColSeq
。例如,
board.set(1, 1, "aa");
board.set(1, 3, "aa");
board.set(0, 1, "B");
board.set(0, 2, "B");
board.set(1, 0, "aa");
列表应按以下方式排序:
[0,1,"B"], [0,2,"B"], [1,0,"aa"], [1,1,"aa"], [1,3,"aa"]
但我的代码给了我无限循环。 smb能不能告诉我我的代码是什么?
假设列表不为空!
LinkedList<RowColElem<T>> rowColElems; //contains some info
private void sortedRowColSeq(int row, int col, T x){
RowColElem<T> object = new RowColElem<T>(row, col, x);
ListIterator<RowColElem<T>> iter = rowColSeq.listIterator();
while(iter.hasNext()){
RowColElem<T> inListObject = iter.next();
if(object.getRow() < inListObject.getRow()){
iter.previous();
iter.add(object);
}
else if(object.getRow() == inListObject.getRow()){
if(object.getCol() < inListObject.getCol()){
iter.previous();
iter.add(object);
}
else{
iter.add(object);
}
}
else{
iter.add(object);
}
}
}
RowColElem<T>
课程
public class RowColElem<T>{
private int row;
private int col;
private T elem;
// Create a RowColElem with the parameter parts
public RowColElem(int r, int c, T e){
this.row = r;
this.col = c;
this.elem = e;
}
// Return the row
public int getRow(){
return this.row;
}
// Return the column
public int getCol(){
return this.col;
}
// Return the element
public T getElem(){
return this.elem;
}
// Return a pretty string version of the triple formated as
// (row,col,elem)
public String toString(){
return String.format("(%d,%d,%s)",row,col,elem);
}
@SuppressWarnings("unchecked")
// Perform a deep equality check between this RowColElem and another
// object
public boolean equals(Object other){
if(other == null || !(other instanceof RowColElem)){
return false;
}
RowColElem<T> that = (RowColElem<T>) other;
return
this.row == that.row &&
this.col == that.col &&
this.elem.equals(that.elem);
}
}
答案 0 :(得分:2)
您的基本问题是您编写了排序代码。解决方案是单线:
minigame
答案 1 :(得分:1)
我只能说,<select>
和你的代码一样,给出了无限循环。无论如何,你每次都会放一个新的元素。你应该有一个不会造成比赛情况的条件。您的代码补充超出了它手中的代码,这就是竞争条件。