我从txt文件中插入数千个数字到列表中,我想每5个数字对它们进行排序.这是可能的,如果是的话,如何做到这一点?
public static void readFromFile(){
List<Integer> putInList = new ArrayList<Integer>();
int jNum;
TextIO.readFile("jokerNums.txt");//read from a spesific file.
try{
do{
jNum = TextIO.getInt();
putInList.add(jNum);
} while(!TextIO.eof());// Test whether the next character in
//the current input source is an end-of-file
}
catch(IllegalArgumentException e){
}
TextIO.put(putInList);
}
我已经在do {} while循环中尝试了for循环,但是它的无限.
仅举几个插入的数字是9785.
先感谢您.
最佳答案
for (int i = 0; !TextIO.eof(); i++){
int value = TextIO.getInt();
int targetIndex = (i/5)*5;
for (; targetIndex < putInList.size(); targetIndex++)
{
if(putInList.get(targetIndex)>value)
{
break;
}
}
putInList.add(targetIndex,value);
}