1 2 3 4 5 6 7 8 9 10 11 | package exercise; import java.util.Arrays; public class Ex13_4_1 { public static void main(String[] args) { String[] words = { "book" , "paper" , "book" , "pencil" , "note" , "eraser" }; Arrays.sort(words); for (String s : words) { System.out.println(s); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 | package exercise; import java.util.Arrays; import lib.Input; public class Ex13_4_2 { public static void main(String[] args) { String[] words = { "book" , "paper" , "book" , "pencil" , "note" , "eraser" }; Arrays.sort(words); String key = Input.getString(); int index = Arrays.binarySearch(words, key); System.out.println( "検索結果=" + index); } } |
1 2 3 4 5 6 7 8 9 10 11 | package exercise; import java.util.Arrays; public class Ex13_4_3 { public static void main(String[] args) { String[] words = { "book" , "paper" , "book" , "pencil" , "note" , "eraser" }; Arrays.stream(words) .distinct() .sorted() .forEach(w->System.out.println(w)); } } |
1 2 3 4 5 6 7 8 9 10 | package exercise; import java.util.Arrays; public class Ex13_4_4 { public static void main(String[] args) { double [] data = new double [ 5 ]; Arrays.fill(data, 5.1 ); double [] dataCopy = Arrays.copyOf(data, 10 ); System.out.println(Arrays.toString(dataCopy)); } } |
1 2 3 4 5 6 7 8 9 10 11 | package exercise; import java.util.Arrays; public class Ex13_4_5 { public static void main(String[] args) { double [] data={ 2.8 , - 3.3 , 1.5 , - 5.2 , 4.2 , 8.1 }; double total = Arrays.stream(data) .filter(d->d> 0 ) .sum(); System.out.println( "合計=" + total); } } |