勉強メモ@プロJava/その2
項番10.1.4 p.195練習問題なり
package projava; import java.util.ArrayList; import java.util.List; public class StreamExercise { public static void main(String[] args) { System.out.println("--練習1:5文字ちょうどの文字列を表示する--"); var fruits = List.of("apple","banana","grape"); for (String fruit : fruits) { if (fruit.length() == 5){ System.out.println(fruit); } } System.out.println("--練習2:5文字ちょうどの文字列を取り出した新たなListを作る--"); var newFruits = new ArrayList<String>(); for (String fruit : fruits) { if (fruit.length() == 5){ newFruits.add(fruit); } } System.out.println(newFruits); System.out.println("--練習3:5文字ちょうどの文字列の個数を数える--"); int count = 0; for (String fruit : newFruits) { count++; } System.out.println("5文字ちょうどの文字列の個数は:" + count); System.out.println("--練習4:5文字ちょうどの文字列全てが「p」を含むか確認する--"); boolean allContainsFlg = false; for (String fruit : newFruits) { if(!fruit.contains("p")){ allContainsFlg = false; break; } allContainsFlg = true; } if (allContainsFlg){ System.out.println("5文字ちょうどの文字列全てが「p」を含む"); }else{ System.out.println("5文字ちょうどの文字列のうち少なくとも一つは「p」を含まない"); } System.out.println("--練習5:5文字ちょうどの文字列のどれか一つでも「p」を含むか確認する--"); boolean someContainsFlg = false; for (String fruit : newFruits) { if(fruit.contains("p")){ someContainsFlg = true; break; } } if (someContainsFlg){ System.out.println("5文字ちょうどの文字列のうち少なくとも一つは「p」を含む"); }else{ System.out.println("5文字ちょうどの文字列のいずれも「p」を含まない"); } } }
▼コンソール うむ!「この問題わかりそうだから飛ばそかな」って思ったけど書いてみてよかった、 まだまだ自分にはやっぱ書くのに時間がかかる、ということがわかった。うむ、収穫収穫である! 寝よー