我使用Arrays.sort
方法來排序我自己的Comparable
對象的數組。在使用sort之前,數組已滿,但在對數組進行排序並將其打印到System之後,沒有任何內容正在打印出來。編輯。陣列根本沒有打印任何東西。不是空行,只是沒有。使用Arrays.sort,空數組返回
這裏是我的方法,它使用sort
代碼:
public LinkedQueue<Print> arraySort(LinkedQueue<Print> queue1)
{
Print[] thing = new Print[queue1.size()];
LinkedQueue<Print> newQueue = new LinkedQueue<Print>();
for(int i = 0; i <queue1.size(); i++)
{
Print ob = queue1.dequeue();
thing[i] = ob;
System.out.println(thing[i]); //printing works here
}
Arrays.sort(thing);
for(int j = 0;j<thing.length-1;j++)
{
System.out.println(thing[j]); //printing does not work here
newQueue.enqueue(thing[j]);
}
return newQueue;
}
,這裏是名爲Print
的Comparable
對象的類。
public class Print implements Comparable<Print>
{
private String name;
private int numPages,arrivalTime,startTime,endTime;
public Print(String n, int p, int time, int sTime, int eTime)
{
name = n;
numPages = p;
arrivalTime = time;
startTime = sTime;
endTime = eTime;
}
public int getPages()
{
return numPages;
}
public int compareTo(Print other)
{
if(this.getPages()<other.getPages())
return -1;
else if(this.getPages()>other.getPages())
return 1;
else
return 0;
}
public String toString()
{
return name+"("+numPages+" pages) - printed "+startTime+"-"+endTime+" minutes";
}
}
爲什麼你不打印出最後一個元素? 'for(int j = 0; j
另外,「打印在這裏不起作用」是什麼意思?是*什麼*被打印?錯誤的值? –
由於您在'Print'類中覆蓋了'toString',因此您可以使用'Arrays.toString(thing)'進行打印。並告訴我們輸出的是什麼。 –