Во втором цикле я хочу почистить массив от строк, которые не являются самыми длинными, но почему-то ничего не выходит :(
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
strings = new ArrayList<String>();
BufferedReader chtec = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 5; i++) {
strings.add(i, chtec.readLine());
}
for (int i = 0; i < strings.size(); i++){
if (strings.get(i).length() < strings.get(i + 1).length() && i != strings.size() - 1)
strings.remove(i);
if (strings.size() > 1 && i == strings.size() - 1 && strings.get(i).length() < strings.get(i - 1).length())
strings.remove(i);
if (strings.size() > 1 && i == strings.size() - 1 && strings.get(i).length() > strings.get(i - 1).length()) {
for (int b = 0; b < strings.size(); b++)
if (b != strings.size() - 1)
strings.remove(b);
}
if (strings.size() == 1 && i == strings.size() - 1)
break;
}
for (int i = 0; i < strings.size(); i++){
System.out.println(strings.get(i));
}
}
}package com.javarush.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Самая длинная строка
*/
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
strings = new ArrayList<String>();
BufferedReader chtec = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> text = new ArrayList<String>();
String textik = "";
for (int i = 0; i < 5; i++) {
strings.add(i, chtec.readLine());
if (i > 0) {
if (strings.get(i - 1).length() < strings.get(i).length())
text.add(strings.get(i));
if (strings.get(i - 1).length() == strings.get(i).length())
text.add(strings.get(i));
}
if (i == 0)
text.add(strings.get(i));
}
int i = text.size();
while (i > 0) {
if (text.get(i).length() < text.get(i - 1).length())
text.remove(i);
i--;
}
for (int i1 = 0; i1 < text.size(); i1++)
{
System.out.println(text.get(i1));
}
}
}