猿问

移动字符串数组中的元素,直到所选字母位于索引 0

我有一个字符串数组,如 first_page = {{"U","M","Y","Q","I","A","L","D","P","F","E","G","T","Z","V","W","H","O","X","J","C","R","B","S","N","K"}


现在,如果我输入A,它会搜索A(A字符串中只有一个)并不断将A每个元素向上移动一个索引,最后一个索引下的元素被移动到 index 0,而A不在 index 0。


实现它的最简单方法是什么?我遇到了麻烦,我目前不太工作的代码如下:


   for(int i = 0; i<25;i++) {

        help[i] = first_page[keys_int[0]][i];

                  } 

    for(int i = 0; i<25;i++) {


       if(first_page[keys_int[0]][i].equals(plain_chars[0].toUpperCase())) {

           rememberedi = i;

          int k = i;


            do {


                first_page_copy[keys_int[0]][k+1] = help[k];

                first_page_copy[keys_int[0]][k] = help[k-1];


                k++;

                if(k==24) {

                    first_page_copy[keys_int[0]][k+1] = help[k];

                          first_page_copy[keys_int[0]][0] = help[24];

                          k = 1;

                }


              }while(!first_page[keys_int[0]][0].equals(plain_chars[0]));

            i = 26;

       }

    }    

该数组是多维的,但我只处理所选列中的一行。


我将数组中的整行复制到数组中help,然后继续用当前位置替换下一个元素,用上一个替换当前元素。感谢任何回应。



收到一只叮咚
浏览 139回答 3
3回答

翻过高山走不出你

正如其他答案已经说过的那样,第一步是找到应该移动到 position 的字符的索引0。private static int indexOf(char character, char[] characters) {&nbsp; &nbsp; for (int i = 0; i < characters.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (characters[i] == character) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return -1;}然后我们可以使用这些类Arrays并System以快速的方式执行转换。protected static void shiftRight(char character, char[] characters) {&nbsp; &nbsp; int indexOf = indexOf(character, characters);&nbsp; &nbsp; if (indexOf > 0) {&nbsp; &nbsp; &nbsp; &nbsp; char[] temp = Arrays.copyOfRange(characters, 0, indexOf);&nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(characters, indexOf, characters, 0, characters.length - indexOf);&nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(temp, 0, characters, characters.length - temp.length, temp.length);&nbsp; &nbsp; }}如果indexOf是小于0的character没有被发现。如果indexOf是0则characters不需要移位,因为数组已经具有所需的状态。在这两种情况下都不会发生转变。将此应用于问题中的字符:public static void main(String[] args) throws Exception {&nbsp; &nbsp; char character = 'A';&nbsp; &nbsp; char[] characters = { 'U', 'M', 'Y', 'Q', 'I', 'A', 'L', 'D', 'P', 'F', 'E', 'G', 'T', 'Z', 'V', 'W', 'H', 'O', 'X', 'J', 'C', 'R', 'B', 'S', 'N', 'K' };&nbsp; &nbsp; System.out.println(Arrays.toString(characters));&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; shiftRight(character, characters);&nbsp; &nbsp; System.out.println(Arrays.toString(characters));}这打印:[U, M, Y, Q, I, A, L, D, P, F, E, G, T, Z, V, W, H, O, X, J, C, R, B, S, N, K]&nbsp;[A, L, D, P, F, E, G, T, Z, V, W, H, O, X, J, C, R, B, S, N, K, U, M, Y, Q, I]请注意:我使用的是一个数组,String而不是问题中使用的数组,char因为每个数组String只包含一个字母。

慕莱坞森

这听起来像是很多不必要的转变。如果我做对了,请告诉我:例如:{A,B,C,D,E,F,G} - 类型 'E' - 结果:{E,F,G,A,B,C,D}在这种情况下:只需先找到 'E' 的索引,然后您就可以执行 for- 循环(不需要 do - while)for(int i=0; i<source.length; i++){&nbsp; &nbsp; target[i] = source[(i+index)%source.length];}

交互式爱情

数组移位算法非常简单。最好在示例中展示它:初始数组&nbsp;{'a', 'b', 'c', 'd', 'e'}您想'c'成为第一个元素,因此将数组向左移动以获取offs = 2位置第 1 步:反转数组中的所有元素:{'e', 'd', 'c', 'b', 'a'}第 2 步:反转第一个3元素arr.length - offs = 5 - 2 = 3:{'c', 'd', 'e', 'b', 'a'}第 3 步:反转最后一个2元素offs = 2:{'c', 'd', 'e', 'a', 'b'}在这里,您已经将给定的数组移动了 2 个位置。您可以就地完成所有这些操作,而无需创建临时数组。这是一个很好的方法,特别是对于巨大的数组。public static void shiftArray(char[] arr, char ch) {&nbsp; &nbsp; int pos = indexOf(arr, ch);&nbsp; &nbsp; if (pos > 0) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0, j = arr.length - 1; i < j; i++, j--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(arr, i, j);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0, j = arr.length - pos - 1; i < j; i++, j--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(arr, i, j);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = arr.length - pos, j = arr.length - 1; i < j; i++, j--)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(arr, i, j);&nbsp; &nbsp; }}辅助方法:private static int indexOf(char[] arr, char ch) {&nbsp; &nbsp; for (int i = 0; i < arr.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] == ch)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return i;&nbsp; &nbsp; return -1;}private static void swap(char[] arr, int i, int j) {&nbsp; &nbsp; char ch = arr[i];&nbsp; &nbsp; arr[i] = arr[j];&nbsp; &nbsp; arr[j] = ch;}
随时随地看视频慕课网APP

相关分类

Java
我要回答