计算最长英语单词链

这是邹欣老师一篇博文的内容,当时有幸可以看到,今天又被老师作为随堂练测试来进行练习.

这是原文的地址:《计算最长英语单词链》 https://www.cnblogs.com/xinz/p/7119695.html

一、需求分析

老师给我们的要求是:一个文本文件中有N 个不同的英语单词,将首字母和尾字母相同单词进行拼接,然后输出相应的字符串.

例如有以下单词:

Apple

Zoo

Elephant

Under

Fox

Dog

Moon

Leaf

Tree

 

拿到这个问题的之后,首先想到的就是对问题进行分解.我们可以将它分为四个部分:读取文件,将单词首尾字母取出,进行比对并存入数组,写入文件.

二、具体实现

首先,我们是要从文件将内容读取出来,,这个是比较容易的.以下代码可以实现.

 1         File file = new File("E://JAVA//软件工程//WordsLines//input1.txt");
 2         ArrayList<String> arrayList = new ArrayList<>();
 3         try {
 4             // int i = 0;
 5             InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file));
 6             BufferedReader bf = new BufferedReader(inputReader);
 7 
 8             // 按行读取字符串
 9             String str;
10             while ((str = bf.readLine()) != null) {
11                 arrayList.add(str);
12                 // i++;
13             }
14             // if (i == 0) {//判断文本为空
15             // System.out.println("文本文件为空");
16             // System.out.println("程序运行出错,程序结束");
17             // System.exit(0);
18             // }
19             // if (i == 1) {//判断只有单个单词
20             // System.out.println("文件单词只有一个");
21             // System.out.println("程序运行出错,程序结束");
22             // System.exit(0);
23             // }
24             bf.close();
25             inputReader.close();
26         } catch (IOException e) {
27             e.printStackTrace();
28         }
29         // 对ArrayList中存储的字符串进行处理
30         String[] array = new String[arrayList.size()];
31         for (int i = 0; i < arrayList.size(); i++) {
32             array[i] = arrayList.get(i);
33         }
34         // for (int i = 0; i < length; i++) {
35         // System.out.println(array[i] + "\n");
36         // }

  在读取到文件内容并存储到数组中后,我们要将各个单词中的收尾字母取出.在JAVA中(JavaScript),提供了subString方法,使我们可以提取字符串中介于两个指定下标之间的字符,这就给我们一个很好的方法去提取每个单词中的字符了.但是需要注意的是,在subString中是不可以接收负的参数的.关于他的使用方法,可以参考JavaScript substring() 方法.

那么我们就可以对他进行操作了.

通过 1 array[i].length() - 1, array[i].length() 的范围来找到单词中的最后一个字母,通过 1 array[j].substring(0, 1) 的范围来找到单词中的第一个字母.然后通过比对将他们存入数组.

 1 String a, b, str;
 2         String[] strresult = new String[arrayList.size()];
 3         for (int i = 0; i < arrayList.size(); i++) {
 4             str = array[i];
 5             a = array[i].substring(array[i].length() - 1, array[i].length());
 6             for (int j = 0; j < arrayList.size(); j++) {
 7                 b = array[j].substring(0, 1);
 8                 if (array[i].equals(array[j]) == false && a.equals(b)) {
 9                     str = str + array[j];
10                     a = array[j].substring(array[j].length() - 1, array[j].length());
11                 }
12                 b = null;
13             }
14             strresult[i] = str;
15         }

最后将得到的strresult数组中的内容存入文本文件就可以了.

三、思考

  最基本的方法就是这么简单,但是,如果我们遇到了一个超大的英文文本,或者是超大的英文文章该怎么办呢?会不会程序崩溃?会不会出现溢出?

  又或者是当我们储存的文本过小,又会出现什么情况呢?

  再或者是当我们输入的内容不只是英文,存在其他的语言,比如汉语,数字呢?

  这些都是值得思考的问题.

猜你喜欢

转载自www.cnblogs.com/yandashan666/p/10987036.html