今天软工课上老师留的作业总结一下
1 import java.io.*; 2 import java.util.*; 3 4 public class Maxlist { 5 6 public static void main(String[] args)throws FileNotFoundException, IOException { 7 8 9 File file=new File("input.txt"); 10 File file2=new File("output.txt"); 11 12 13 if(!file.exists()) 14 { 15 System.out.println("文件不存在!"); 16 } 17 else if(file.exists() && file.length() == 0) { 18 System.out.println("文件为空!"); 19 } //判断是否文件为空 20 else 21 { 22 BufferedReader br=new BufferedReader(new FileReader(file)); 23 BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt")) ; 24 25 26 String s; 27 int i,j; 28 ArrayList<ArrayList<String>> listall=new ArrayList(); 29 String key; 30 s=br.readLine(); 31 //将文本文件中的所有信息变为字符串 32 String []s1=s.split(" ");//以空格为分割划分每个单词,s1为所有单词的数组 33 if(s1.length==1) 34 System.out.println("单词数为1"); 35 else 36 { 37 38 for(i=0;i<s1.length;i++) 39 { 40 System.out.println(s1[i]); 41 }//测试录入 42 43 for(j=0;j<s1.length;j++) 44 { 45 ArrayList<String> list=new ArrayList<String>(); 46 key=s1[j].substring(s1[j].length()-1);//取出每个单词最后一个字母 47 list.add(s1[j]);//把当前单词加入list 48 System.out.println("key:"+key); 49 for(i=1;i<s1.length;i++) 50 { 51 52 if(key.equals(s1[i].substring(0, 1))) 53 { 54 list.add(s1[i]); 55 key=s1[i].substring(s1[i].length()-1); 56 } 57 else 58 continue; 59 } 60 listall.add(list); 61 } 62 int max=0; 63 int count=0; 64 65 for(i=0;i<listall.size();i++) 66 { 67 if(max<listall.get(i).size()) 68 { 69 max=listall.get(i).size(); 70 count=i; 71 } 72 } 73 System.out.println("排位"+count); 74 System.out.println("最长单词链为:"); 75 for(i=0;i<listall.get(count).size();i++) 76 { 77 System.out.println(listall.get(count).get(i)); 78 bw.write(listall.get(count).get(i)); 79 bw.write(" "); 80 } 81 br.close(); 82 bw.close(); 83 } 84 } 85 } 86 87 }
标签:英语单词,测试题,s1,System,课上,file,println,new,out From: https://www.cnblogs.com/rsy-bxf150/p/17162010.html