import java.util.Arrays; import java.util.function.BiFunction; import java.util.function.UnaryOperator; public class Task { public static void main(String[] args) { String[] words = args.clone(); if (words.length == 0) { words = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}; } // 把所有字母都转换成大写 handleList(words, new UnaryOperator<String>() { @Override public String apply(String s) { return null; } }); System.out.println(Arrays.toString(words)); // 把每个条目根据其索引位置进行重复 handleList2(words, new BiFunction<String, Integer, String>() { @Override public String apply(String s, Integer count) { return null; } }); System.out.println(Arrays.toString(words)); } public static void handleList(String[] list, UnaryOperator<String> operator) { for (int i = 0; i < list.length; i++) { list[i] = operator.apply(list[i]); } } public static void handleList2(String[] list, BiFunction<String, Integer, String> operator) { for (int i = 0; i < list.length; i++) { list[i] = operator.apply(list[i], i); } } }handleList方法 将list中的每个元素转为大写 例如["a", "b", "c"]可以得到["A", "B", "C"]handleList2方法 将list中的每个元素都会根据其位置索引作为个数,进行复制 例如["a", "b", "c"]可以得到["", "b", "cc"]
时间: 2023-05-19 15:04:33 浏览: 128
java代码-import java.util.Scanner;
5星 · 资源好评率100%
这段代码的作用是将一个字符串数组中的元素转换为大写,并且将每个元素根据其位置索引作为个数进行复制。其中,handleList方法将list中的每个元素转为大写,而handleList2方法将list中的每个元素都会根据其位置索引作为个数,进行复制。这个程序的输出结果是:["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]和["", "B", "CC", "DDD", "EEEE", "FFFFF", "GGGGGG", "HHHHHHH", "IIIIIIII", "JJJJJJJJJ"]。
阅读全文