Cats萌萌
看起来您走在正确的轨道上,但您实际上应该交换数组中的元素。另外,不要返回String. 并遵循 Java 命名约定。喜欢,public static void swapString(String[] arr) { for (int i = 0; i + 1 < arr.length; i += 2) { String t = arr[i + 1]; arr[i + 1] = arr[i]; arr[i] = t; }}然后像这样调用/测试它,public static void main(String[] args) { String[] arr = { "Cecilia", "Jasmine", "David", "John", "Sylvia", "Bill", "Austin", "Bernardo", "Christopher", "Leticia", "Ronaldo" }; System.out.println(Arrays.toString(arr)); swapString(arr); System.out.println(Arrays.toString(arr));}我得到(按要求)[Cecilia, Jasmine, David, John, Sylvia, Bill, Austin, Bernardo, Christopher, Leticia, Ronaldo][Jasmine, Cecilia, John, David, Bill, Sylvia, Bernardo, Austin, Leticia, Christopher, Ronaldo]