猿问

如何在Java中交换字符数组(就地)

所以基本上没有能够/被允许制作一个新数组。无法返回任何内容,但实际上更改和操作当前数组。您如何获取一组字符并简单地翻转/反转它们。


Starting array: ['P','e','r','f','e','c','t',' ','M','a','k','e','s',' ','P','r','a','c','t','i','c','e']

反转每个以空格分隔的单词


Reversed: ['P','r','a','c','t','i','c','e',' ','M','a','k','e','s',' ','P','e','r','f','e','c','t'] 

这是我到目前为止所拥有的


代码:


  class Main {

     public static void main(String[] args) {

       char[] charArr = new char[] {'P','e','r','f','e','c','t',' ','M','a','k','e','s',' ','P','r','a','c','t','i','c','e'};

       reverseCharArray(charArr);

     }



     public static void reverseCharArray() {

       int arrLength = charArr.length;

       for (int i = 0; i <= arrLength / 2; i++) {

         charArr[arrLength - i - 1] = charArr[i];

         System.out.println(charArr);

       }

     }

   }

更新:好的,我发现就是这样。我需要做的实际上是交换字符的句子拼写的单词。使句子倒转/倒转。


开满天机
浏览 220回答 4
4回答

芜湖不芜

这绝对不是一个好的解决方案,但它是一个有效的解决方案。class Main {public static void main(String[] args) {&nbsp; &nbsp; char[] charArr = new char[] { 'P', 'e', 'r', 'f', 'e', 'c', 't', ' ', 'M', 'a', 'k', 'e', 's', ' ', 'P', 'r',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'a', 'c', 't', 'i', 'c', 'e' };&nbsp; &nbsp; System.out.println(charArr);&nbsp; &nbsp; reverseCharArray(charArr,0);&nbsp; &nbsp; System.out.println(charArr);}public static void reverseCharArray(char[] charArr, int sorted) {&nbsp; &nbsp; /* Look for last space*/&nbsp; &nbsp; int lastSpace = -1;&nbsp; &nbsp; for (int i = 0; i < charArr.length; i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (charArr[i] == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastSpace = i;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /* Grab the word and move it at the beginning of the sorted array */&nbsp; &nbsp; for (int i = lastSpace + 1; i < charArr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int k = i;&nbsp; &nbsp; &nbsp; &nbsp; while (k != sorted) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char tmp = charArr[k-1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charArr[k-1] = charArr[k];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charArr[k] = tmp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k--;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sorted++;&nbsp; &nbsp; }&nbsp; &nbsp; /* At this point, the last character is a space*/&nbsp; &nbsp; /* Else, we've swapped all the words */&nbsp; &nbsp; int k = charArr.length - 1;&nbsp; &nbsp; if (charArr[k] != ' ') {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; /* If it's a space, grab it and move it at the beginning*/&nbsp; &nbsp; while (k != sorted) {&nbsp; &nbsp; &nbsp; &nbsp; char tmp = charArr[k-1];&nbsp; &nbsp; &nbsp; &nbsp; charArr[k-1] = charArr[k];&nbsp; &nbsp; &nbsp; &nbsp; charArr[k] = tmp;&nbsp; &nbsp; &nbsp; &nbsp; k--;&nbsp; &nbsp; }&nbsp; &nbsp; sorted++;&nbsp; &nbsp; /*Recursive call on the not sorted array*/&nbsp; &nbsp; reverseCharArray(charArr,sorted);}}

临摹微笑

下面的方法交换间隔。请注意,它们必须具有相同的长度。public static char[] swap(char[] arr, int lstart, int rstart, int len){&nbsp; &nbsp; &nbsp; for(int i=lstart; i<lstart+len; i++){&nbsp; &nbsp; &nbsp; &nbsp; char temp = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; arr[i] = arr[rstart+i];&nbsp; &nbsp; &nbsp; &nbsp; arr[rstart+i] = temp;&nbsp; &nbsp;}&nbsp; &nbsp;return arr;}

慕村9548890

假设您有以下数组;[h, e, y,&nbsp; , y, o, u]你必须以一种模式工作;从外到内(或相反)。因此,[1,2,3,4,3,2,1]您必须交换1and 1,2依此2类推。如您所见,该数组的长度为 7,在这种情况下,所需的交换量正好是 4(4与其自身交换)。要计算交换量,您可以简单地将数组长度除以 ceil 2.0f。现在你必须遍历数组,交换那些索引。要计算要交换的索引,您必须检查您的交换位置。假设您在第二次交换中,2数组中的索引是 1 和 5,的索引3是 2 和 4。您现在可能已经识别出这种模式。第一个索引始终是已完成交换的数量,第二个是数组的长度减去 1 减去已完成交换的数量。这是放入代码的;&nbsp; &nbsp; public static void swap(char[] array){&nbsp; &nbsp; &nbsp; &nbsp; int totalSwaps = (int) Math.ceil(array.length / 2.0f);&nbsp; &nbsp; &nbsp; &nbsp; for(int currentSwaps = 0; currentSwaps < totalSwaps; currentSwaps++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char char1 = array[currentSwaps];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int position2 = array.length - (currentSwaps + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[currentSwaps] = array[position2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[position2] = char1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(array));&nbsp; &nbsp; }编辑:我刚刚看到您要求反转 char[] 中的每个单词,您可能想在第一句话中澄清这一点去做这个; 我建议您使用String::split将字符串拆分为 string[] 并将String::toCharArray其更改为字符数组。虽然这确实创建了新的数组

小怪兽爱吃肉

有一个算法,如下,最初,将给定字符串的单个单词一一反转,对于提供的示例"Perfect Makes Practice",在反转单个单词之后,字符串应该是“tcefreP sekaM ecitcarP”。"Practice Makes Perfect"在上面的示例中,从头到尾反转整个字符串以获得所需的输出。更多检查Reverse words in a given string。
随时随地看视频慕课网APP

相关分类

Java
我要回答