思路:首先读取文件,将文件中的单词用动态字符串数组存储,然后关闭文件,得到数组后,创立一个死循环,运用循环语句,依次让单词和单词之间的首尾单词相互判断,看是否能匹配成功,成功则将两数存到一个数组中,匹配结束后设定一个数加一,该数到一定大小后跳出循环,之后让数组长度比较大小,得出最大的数组,输出。
package wtf2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.Scanner;
public class was {
public static String last(String a) {
return a.substring(a.length()-1);}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\zhang san\\eclipse-workspace\\wtf\\1023.txt"));
String readLine = null;
List<String> lists = new ArrayList<String>();
while((readLine = br.readLine()) != null){
String[] wordsArr1 = readLine.split("[^a-zA-Z]");
for (String word : wordsArr1) {
if(word.length() != 0){
lists.add(word);
}
}
}
br.close();
String [] a=new String[lists.size()];
for(int i=0;i<lists.size();i++)
{a[i]=lists.get(i);}
int m=0;
for(int i=0;i<lists.size();i++)
{int p;p=i;
while(m!=4) {
System.out.println(a[p]+" ");
String q=a[p].substring(a[p].length()-2,a[p].length()-1);
a[p]=null;
for(int j=0;j<lists.size();j++)
if(j!=lists.size()-1)
{String q1=null;
q1=last(a[j]);
if(q.equals(q1))
{p=j;m++;break;}}
else {m=4;break;}
}
}
}}
标签:util,java,String,int,lists,单词,接龙,import From: https://www.cnblogs.com/srz123/p/17178912.html