生成二進制文件(使用DataOutputStream,使用緩沖區(qū),耗時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("循環(huán)完成"); stream.flush(); stream.close(); time?=?System.currentTimeMillis()?-?time; System.out.println(time+"毫秒");
分析文件(使用DataInputStream,使用緩沖區(qū),耗時65秒)
a.讀取前100個整數(shù)
b.排序,把排序后的數(shù)組看成堆,最小值在根節(jié)點
c.遍歷整個文件,把讀到的數(shù)和最小值比較,如果比最新值小,則丟棄,如果比最小值大則替換最小值重建堆。
d.文件讀取完畢,堆中的元素就是要找的100個最大值,再執(zhí)行一次排序。
TestRead.Javapublic?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培訓(xùn)機構(gòu)的小編針對“2020年Java易寶支付筆試題”的內(nèi)容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。
Java筆試題