字符串的一个处理程序

题:
编写一程序输出一个由若干单词构成的字符串中的最长单词及其长度,若字符串中有多个单词满足条件须将这些单词全部输出。(注:程序书写要规范)

import java.util.*;

class MaxLenWord {
  private ArrayList result = new ArrayList();
  private int maxLen = 0;
 
  public MaxLenWord(String str) {
    int maxlenWordIndex = 0;
    String[] strArr = str.split(" ");
    for(int i = 1; i < strArr.length; i++) {
      if(strArr[i].length()>strArr[maxlenWordIndex].length()) {
        maxlenWordIndex = i;
        result.clear();
        result.add(strArr[i]);
      } else if(strArr[i].length()==strArr[maxlenWordIndex].length()) {
        maxlenWordIndex = i;
        result.add(strArr[i]);
      }
    }
  }

  public ArrayList getMaxLenWord() {
    return result;
  }

  public int getMaxlen() {
    Iterator it = result.iterator();

    if(it.hasNext()) {
      maxLen= ((String)it.next()).length();
    }
    return maxLen;
  }
 
  public static void main(String[] args) {
    String str= "The indictment said the defendants had collected geographical geographical geographical geographical data

indicating thousands of people would be killed in the chemical blast";
    MaxLenWord  mlw = new MaxLenWord(str);
    ArrayList maxLen = mlw.getMaxLenWord();
    Iterator test = maxLen.iterator();
    while(test.hasNext()) {
      System.out.println((String)test.next());
    }
    System.out.println(mlw.getMaxlen());
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值