Advertisement
FedchenkoIhor

Самая длинная строка ArrayList

Apr 21st, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package com.javarush.test.level0.lesson0.task0;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6.  
  7. /* Самая длинная строка
  8. 1. Создай список строк.
  9. 2. Считай с клавиатуры 5 строк и добавь в список.
  10. 3. Используя цикл, найди самую длинную строку в списке.
  11. 4. Выведи найденную строку на экран.
  12. 5. Если таких строк несколько, выведи каждую с новой строки.
  13. */
  14. public class Solution {
  15.     public static void main (String[] args) throws Exception {
  16.         ArrayList<String> arrayList = new ArrayList<String> ();
  17.         BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
  18.         for (int i = 0; i < 5; i++) {
  19.             arrayList.add (reader.readLine ());
  20.         }
  21.         printIndexesByWidth (arrayList,getLengthLongest (arrayList));
  22.     }
  23.  
  24.     public static int getLengthLongest (ArrayList<String> input) {
  25.         int result = 0;
  26.         for (int i = 0; i < input.size (); i++) {
  27.             if (result < input.get (i).length ()) {
  28.                 result = input.get (i).length ();
  29.             }
  30.         }
  31.         return result;
  32.     }
  33.  
  34.     public static void printIndexesByWidth (ArrayList<String> input, int width) {
  35.         for (String element : input){
  36.             if (element.length () == width ){
  37.                 System.out.println (element);
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement