我有一个像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
和每个元素通过向上一个索引,最后一个索引下的元素将移至索引0
,而A
不在索引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
中,然后继续用current替换当前所在的下一个元素,并使用前一个替换当前的元素。
感谢您的任何答复。
答案 0 :(得分:2)
正如其他答案已经说过的那样,第一步是找到应该移到位置0
的字符的索引。
private static int indexOf(char character, char[] characters) {
for (int i = 0; i < characters.length; i++) {
if (characters[i] == character) {
return i;
}
}
return -1;
}
然后,我们可以使用类Arrays
和System
来快速执行移位。
protected static void shiftRight(char character, char[] characters) {
int indexOf = indexOf(character, characters);
if (indexOf > 0) {
char[] temp = Arrays.copyOfRange(characters, 0, indexOf);
System.arraycopy(characters, indexOf, characters, 0, characters.length - indexOf);
System.arraycopy(temp, 0, characters, characters.length - temp.length, temp.length);
}
}
如果indexOf
小于0
,则找不到character
。如果indexOf
为0
,则characters
不需要移位,因为数组已经具有所需的状态。在这两种情况下都不会发生移位。
将其应用于问题中的字符:
public static void main(String[] args) throws Exception {
char character = 'A';
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' };
System.out.println(Arrays.toString(characters));
shiftRight(character, characters);
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]
[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
仅包含一个字母。
答案 1 :(得分:0)
这听起来好像很多不必要的转变。 请告诉我是否正确: 例如。: {A,B,C,D,E,F,G}-输入'E'-结果:{E,F,G,A,B,C,D}
在这种情况下:只需先找到'E'的索引,然后就可以进行for循环(无需做-while)
for(int i=0; i<source.length; i++){
target[i] = source[(i+index)%source.length];
}
答案 2 :(得分:0)
数组移位算法非常简单。最好在示例中显示:
{'a', 'b', 'c', 'd', 'e'}
'c'
成为第一个元素,因此将数组向左移动offs = 2
个位置{'e', 'd', 'c', 'b', 'a'}
3
个元素arr.length - offs = 5 - 2 = 3
:{'c', 'd', 'e', 'b', 'a'}
2
个元素offs = 2
:{'c', 'd', 'e', 'a', 'b'}
您可以就地完成所有这些操作,而无需创建临时数组。这是个好方法,特别是对于大型数组。
public static void shiftArray(char[] arr, char ch) {
int pos = indexOf(arr, ch);
if (pos > 0) {
for (int i = 0, j = arr.length - 1; i < j; i++, j--)
swap(arr, i, j);
for (int i = 0, j = arr.length - pos - 1; i < j; i++, j--)
swap(arr, i, j);
for (int i = arr.length - pos, j = arr.length - 1; i < j; i++, j--)
swap(arr, i, j);
}
}
Helper方法:
private static int indexOf(char[] arr, char ch) {
for (int i = 0; i < arr.length; i++)
if (arr[i] == ch)
return i;
return -1;
}
private static void swap(char[] arr, int i, int j) {
char ch = arr[i];
arr[i] = arr[j];
arr[j] = ch;
}
答案 3 :(得分:0)
好吧,您的代码中不需要循环。用Java来实现一些耗时的东西,例如数组的复制,要尽可能快。
其中之一是System.arraycopy(
。
知道了这一点,您的目标是在输入数组中找到所需的String
的索引,然后按照所需的方式复制该数组。
可以here找到arraycopy
的API文档。
这是我想出的最快方法:
public class Answer {
private static int findIndexOf(String[] array, String string) {
for (int index = 0; index < array.length; index++)
if (array[index].equals(string))
return index;
return -1;
}
private static String[] shift(String[] input, String string) {
String[] result = new String[input.length];
int offset = findIndexOf(input, string);
if (offset != -1) {
System.arraycopy(input, offset, result, 0, input.length - offset);
System.arraycopy(input, 0, result, input.length - offset, offset);
return result;
} else {
return null;
}
}
public static void main(String[] args) {
//test
String[] input =
{"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"};
String[] desiredOutput =
{"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"};
System.out.println(Arrays.toString(desiredOutput));
System.out.println(Arrays.toString(shift(input, "A")));
}
}
此代码的输出:
[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]
[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]
注意::如果要将结果放在相同的数组中,可以这样做:
input = shift(input, "A");
答案 4 :(得分:0)
仅对 进行移位,直到第一个值等于给定的字符串为止,就可以
private static boolean shiftToString(String[] strings, String search) {
assert strings != null : "null strings";
assert strings.length > 0 : "empty strings";
assert search != null : "null search";
int count = 0;
while (count++ < strings.length) {
if (strings[0].equals(search))
return true; // or count - 1
String last = strings[strings.length-1];
for (int i = strings.length-1; i > 0; i--) {
strings[i] = strings[i-1];
}
strings[0] = last;
}
return false;
}
这没什么花哨的,只是要求的-如果找不到该值,则使用该计数停止。
实际上,这可以通过复制数组的2个部分来解决,但我只是为了测试一下而已
答案 5 :(得分:0)
重新阅读您的问题和评论后,我可以假定您要逐步移动字符串的整个 2D数组。当然,我还假设这种情况下的每一行都是相同的大小。
现在,我决定写另一个答案,其中包括整个代码(也是分步进行的),因此您可以根据需要进行探索和更改。
import java.util.Arrays;
public class Answer {
//testCase - 2D matrix of Strings:
private static final String[][] testCase =
{
{"A", "B", "C", "D", "E", "F", "G"},
{"0", "1", "2", "3", "4", "5", "6"},
{"a", "b", "c", "d", "e", "f", "g"}
};
private static final String testSearch = "C";
//find the index of searchString in the array of strings
private static int findIndexOf(String[] strings, String searchString) {
for (int index = 0; index < strings.length; index++)
if (strings[index].equals(searchString))
return index; //searchString found at index
return -1; //searchString not found
}
//shifting one row by one step
private static String[] shiftToRightByOneStep(String[] strings) {
String[] result = strings;
String last = strings[strings.length - 1];
System.arraycopy(strings, 0, result, 1, strings.length - 1);
result[0] = last;
return result;
}
//shifting matrix by one step
private static String[][] shiftMatrixToRightByOneStep(String[][] matrix) {
String[][] result = matrix;
for (String[] row : matrix) {
shiftToRightByOneStep(row);
}
return result;
}
public static void main(String[] args) {
String[][] myMatrix = testCase;
String find = testSearch;
int howManySteps = myMatrix[0].length - findIndexOf(myMatrix[0], find);
System.out.println("Shifting matrix by " + howManySteps + " steps");
System.out.println("Original matrix:\n" + Arrays.deepToString(testCase) + "\n_____________________________________");
System.out.println("STEPS:");
for (int step = 0; step < howManySteps; step++)
System.out.println(Arrays.deepToString(shiftMatrixToRightByOneStep(myMatrix)) + "\n_____________________________________");
//testing is it original matrix changed
// (of course it is, b/c I used references instead of copies)
System.out.println("Original matrix:\n" + Arrays.deepToString(testCase));
}
}
输出:
将矩阵移动5步
原始矩阵:
[[A,B,C,D,E,F,G],[0,1,2,3,4,5,6],[a,b,c,d,e,f,g ]]
STEPS:
[[G,A,B,C,D,E,F],[6,0,1,2,3,4,5],[g,a,b,c,d,e,f ]]
[[F,G,A,B,C,D,E],[5,6,0,1,2,3,4],[f,g,a,b,c,d,e ]]
[[E,F,G,A,B,C,D],[4,5,6,0,1,2,3],[e,f,g,a,b,c,d ]]
[[D,E,F,G,A,B,C],[3,4,5,6,0,1,2],[d,e,f,g,a,b,c ]]
[[C,D,E,F,G,A,B],[2,3,4,5,6,0,1],[c,d,e,f,g,a,b ]]
原始矩阵:
[[C,D,E,F,G,A,B],[2,3,4,5,6,0,1],[c,d,e,f,g,a,b ]]