二维数组的移动和遍历
程序员文章站
2024-03-15 08:30:35
...
(二维数组移动)
二维数组的左移和遍历
import java.util.Arrays;
import java.util.Scanner;
public class talk {
static char[][] ch = { { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' },
{ 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R' }, { 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ' }
};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Your first number:");
int[] n = new int[2];
for (int i = 0; i < 2; i++) {
n[i] = scanner.nextInt();
}
System.out.println("You input is:" + Arrays.toString(n));
for (int i = 0; i < n[0] - 1; i++) {
char[] temp = ch[0];
for (int j = 0; j < ch.length - 1; j++) {
ch[j] = ch[j + 1];
}
ch[ch.length - 1] = temp;
}
for (int i = 0; i < ch.length; i++) {
System.out.println(Arrays.toString(ch[i]));
}
for (int m = 0; m < n[1] - 1; m++) {
for (int i = 0; i < ch.length; i++) {
char temp = ch[i][0];
for (int j = 0; j < ch[i].length - 1; j++) {
ch[i][j] = ch[i][j + 1];
}
ch[i][ch[i].length - 1] = temp;
}
}
for (int i = 0; i < ch.length; i++) {
System.out.println(Arrays.toString(ch[i]));
}
Scanner sc = new Scanner(System.in);
System.out.println("print your idea");
char[] a = new char[100];
String str;
str = sc.nextLine();
System.out.println(a);
for (int i = 0; i < str.length(); i++) {
a[i] = str.charAt(i);
System.out.println(a[i] + " ");
}
for (int m = 0; m < a.length; m++) {
for (int i = 0; i < ch.length; i++) {
for (int j = 0; j < ch[i].length; j++) {
if (ch[i][j] == a[m]) {
System.out.println(i + "" + j);
}
}
}
}
}
}
没有进行注释,代码也还可以进行优化,数组移动遍历的时间复杂度有点高,可以变低,比如取余。
下一篇: 二维数组右上左下遍历