欢迎来到天天文库
浏览记录
ID:50826887
大小:41.95 KB
页数:4页
时间:2020-03-15
《Java数据结构顺序表的插入与删除.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、importjava.util.*;importjava.io.*;publicclasszhangyue130824218{publicstaticvoidmain(Stringargs[]){FilereadFile=newFile("F://book.txt");Scannerreader=newScanner(System.in);ScannerreaderOne=newScanner(System.in);inti=0,ss=0,j=0;Bookbook[]=newBook[100];Booktemp=newBook();for(i=0;i<100;i++){book[i]=newB
2、ook();}try{FileReaderinOne=newFileReader(readFile);BufferedReaderinTwo=newBufferedReader(inOne);Strings=null;Strings1=null;while((s=inTwo.readLine())!=null){if(j==0){j=j+1;s1=s;}elseif(j==1){StringTokenizerfenxi=newStringTokenizer(s,"");for(i=0;fenxi.hasMoreTokens();i++){if(i==0){book[ss].no=fenxi.n
3、extToken();}elseif(i==1){book[ss].name=fenxi.nextToken();}elseif(i==2){book[ss].price=Integer.parseInt(fenxi.nextToken());}}ss=ss+1;}}//将文件中的数据转换成BookinOne.close();inTwo.close();for(i=0;ibook[i+1].price){temp=book[i];book[i]=book[i+1];book[i+1]=temp;}}System.out.printf("排
4、序后的书的顺序是:");System.out.printf(s1+"");for(i=0;i5、入要插入的位置:");intx1=0;x1=reader.nextInt();for(i=x1-1;i<99;i++){temp=book[i];book[i]=insert;insert=temp;}/*FileOutputStreamfile1=newFileOutputStream(readFile);file1.write(newString("").getBytes());*/FileWritertoOne=newFileWriter(readFile);BufferedWritertoTwo=newBufferedWriter(toOne);toTwo.write(s1);toTw6、o.newLine();for(i=0;i<100;i++){if(book[i].name!=null){toTwo.write(book[i].no+""+book[i].name+""+book[i].price);toTwo.newLine();}elseif(book[i].name==null){break;}}System.out.printf("插入完毕");toTwo.close();toOne.close();FileWritertoOne1=newFileWriter(readFile);BufferedWritertoTwo1=newBufferedWriter(t7、oOne1);System.out.println("请输入你要删除书的书名:");Bookdelete=newBook();delete.name=readerOne.nextLine();for(i=0;i<100;i++){if(delete.name.compareTo(book[i].name)==0){x1=i;break;}}for(i=x1;i<99;i++){book[i]=bo
5、入要插入的位置:");intx1=0;x1=reader.nextInt();for(i=x1-1;i<99;i++){temp=book[i];book[i]=insert;insert=temp;}/*FileOutputStreamfile1=newFileOutputStream(readFile);file1.write(newString("").getBytes());*/FileWritertoOne=newFileWriter(readFile);BufferedWritertoTwo=newBufferedWriter(toOne);toTwo.write(s1);toTw
6、o.newLine();for(i=0;i<100;i++){if(book[i].name!=null){toTwo.write(book[i].no+""+book[i].name+""+book[i].price);toTwo.newLine();}elseif(book[i].name==null){break;}}System.out.printf("插入完毕");toTwo.close();toOne.close();FileWritertoOne1=newFileWriter(readFile);BufferedWritertoTwo1=newBufferedWriter(t
7、oOne1);System.out.println("请输入你要删除书的书名:");Bookdelete=newBook();delete.name=readerOne.nextLine();for(i=0;i<100;i++){if(delete.name.compareTo(book[i].name)==0){x1=i;break;}}for(i=x1;i<99;i++){book[i]=bo
此文档下载收益归作者所有