更新時間:2020-08-14 16:03:33 來源:動力節點 瀏覽2876次
生成二進制文件(使用DataOutputStream,使用緩沖區,耗時79秒):
????File?file?=?new?File("E:\\test.dat");
if?(!file.exists())?{
file.createNewFile();
}
long?time?=?System.currentTimeMillis();
DataOutputStream?stream?=?new?DataOutputStream(new?BufferedOutputStream(new??FileOutputStream(file)));
Random?random?=?new?Random();
long?count?=?200000000;
System.out.println(count+"is?max?long?int?in?java");
int?temp;
for?(long?i?=?0;?i?<?count;?i++)?{
temp?=?random.nextInt();
stream.writeInt(temp);
stream.writeChar(',');
}
System.out.println("循環完成");
stream.flush();
stream.close();
time?=?System.currentTimeMillis()?-?time;
System.out.println(time+"毫秒");
分析文件(使用DataInputStream,使用緩沖區,耗時65秒)
a.讀取前100個整數
b.排序,把排序后的數組看成堆,最小值在根節點
c.遍歷整個文件,把讀到的數和最小值比較,如果比最新值小,則丟棄,如果比最小值大則替換最小值重建堆。
d.文件讀取完畢,堆中的元素就是要找的100個最大值,再執行一次排序。
TestRead.Java
public?static?void?main(String[]?args)?throws?IOException,?InterruptedException?{
File?file?=?new?File("E:\\test.dat");
long?time?=?System.currentTimeMillis();
DataInputStream?stream?=?new?DataInputStream(new??BufferedInputStream(new?FileInputStream(file)));
int?len?=?100;
long?count?=?100;
int?arr[]?=?new?int[100];
for?(int?i?=?0;?i?<?len;?i++)?{
arr[i]?=?stream.readInt();
stream.readChar();
}
Arrays.sort(arr);
print(arr);
int?temp?=?0;
while(true)?{
try?{
???temp?=?stream.readInt();
???stream.readChar();
???count++;
???if(temp?>?arr[0])?{
??? addToheap(arr,temp); ???
???}?else?{
??? continue;
???}
?}?catch(EOFException?ioe)?{
? ??break;
???}
}
stream.close();
time?=?System.currentTimeMillis()?-?time;
System.out.println(time+"毫秒"+":"+count+"個");
Arrays.sort(arr);
print(arr);
}
???static?void?addToheap(int?arr[],?int?temp){
???arr[0]?=?temp;
???int?index?=?0;
???int?left?=?1;?
???int?right?=?2;
???int?minIndex?=?index;
???while?(left?<?arr.length)?{
???if?(arr[index]?>?arr[left])?{
???minIndex?=?left;
???}
???if?(right?<?arr.length?&&?arr[minIndex]?>?arr[right])?{
???minIndex?=?right;
???}
???if?(minIndex?==?index)?{
???break;
???}?else?{
???temp?=?arr[minIndex];
???arr[minIndex]?=?arr[index];
???arr[index]?=?temp;
???index?=?minIndex;
???left?=?2*index?+?1;
???right?=?2*index?+?2;
???}
} ???
}
static?void?print(int[]?aa)?{
??for?(int?i?=?0;?i?<?aa.length;?i++)?{
???System.out.print(aa[i]?+?",");
???if?((i?+?1)?%?10?==?0)?{
????System.out.println();
???}
??}
?}
以上就是動力節點java培訓機構的小編針對“2020年Java易寶支付筆試題”的內容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業老師隨時為你服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習