首页 > 代码库 > JAVA常见算法题(三十)

JAVA常见算法题(三十)

package com.forezp.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;


/**
 * 一个文本中包含多行字符串,输入长度最长的字符串和最短的字符串<br>
 * 
 * @author Administrator
 *
 */
public class ArrayDemo2 {
    public static final boolean FLAG = true;
    
    
    public static void main(String[] args) throws Exception {
        BufferedReader cin;
        String s;
        if(FLAG){
            cin = new BufferedReader(new FileReader("D:\\123.txt"));
        }else{
            cin = new BufferedReader(new InputStreamReader(System.in));
        }
        
        ArrayList<String> al = new ArrayList<String>();
        int max = Integer.MAX_VALUE;
        int min = Integer.MIN_VALUE;
        
        ArrayList<Integer> list = new ArrayList<Integer>();
        while((s = cin.readLine())!=null){
            int len = s.length();
            al.add(s);
            list.add(len);
            /*if (len>max) {
                max = len;
            }
            
            if(len < max){
                min = len;
            }*/
        }
        
         max = Collections.max(list);
         min = Collections.min(list);
        
        
        
        for (String tmp : al) {
            int len = tmp.length();
            if (len == min) {
                System.out.println(tmp);
            }
        }
        
        for (String tmp : al) {
            int len = tmp.length();
            if (len == max) {
                System.out.println(tmp);
            }
        }
        
    }
    
}

 

 

运行结果:

技术分享

 

 

 D盘的文本资料:

技术分享

 

JAVA常见算法题(三十)