Java 在每一行和每一列中创建具有唯一字符的二维字符表
我试图制作一个二维表格,其中的字符不能在列或行中重复。我正在尝试至少做2行数组。我可以做第一个,但其他线路有问题。在第二行,它抛出一个错误-ArrayIndexOutOfBoundsException-11。有人能帮我吗 主要类别:Java 在每一行和每一列中创建具有唯一字符的二维字符表,java,arrays,char,Java,Arrays,Char,我试图制作一个二维表格,其中的字符不能在列或行中重复。我正在尝试至少做2行数组。我可以做第一个,但其他线路有问题。在第二行,它抛出一个错误-ArrayIndexOutOfBoundsException-11。有人能帮我吗 主要类别: import java.util.ArrayList; import java.util.Random; public class Main { char[][] table = new char[26][26]; Random r; public stati
import java.util.ArrayList;
import java.util.Random;
public class Main {
char[][] table = new char[26][26];
Random r;
public static void main(String[] args) {
new Main();
new GlobalVars();
}
public Main() {
r = new Random();
createFirstLine();
createOtherLine(1);
}
public void createFirstLine() {
ArrayList<Integer> intLeft = GlobalVars.cIntToArrL();
int counter = 0;
for(int i = intLeft.size(); i > 0; i--) {
int charPosChosen = r.nextInt(intLeft.size());
int charPosGot = intLeft.get(charPosChosen);
table[0][counter] = GlobalVars.getCharValue(charPosGot);
counter ++;
intLeft.remove(charPosChosen);
}
System.out.println(table[0]);
}
public void createOtherLine(int n) {
ArrayList<Integer> intLeft = GlobalVars.cIntToArrL();
for(int i = 0; i < 26; i++) {
ArrayList<Integer> intLeftForCell = intLeft;
for(int column = 0; column < n; column ++) {
intLeftForCell.remove(
GlobalVars.getNumericValue(table[n][i])
);
}
int charPosChosen = r.nextInt(intLeftForCell.size());
int charPosGot = intLeftForCell.get(charPosChosen);
table[1][i] = GlobalVars.getCharValue(charPosGot);
intLeft.remove((Integer)charPosGot);
}
System.out.println(table[1]);
}
}
GlobalVars类:
import java.util.ArrayList;
public class GlobalVars {
public static String alphStr = "abcdefghijklmnopqrstuvwxyz";
public static char[] alphArr;
public static int[] intArr = new int[26];
public GlobalVars() {
// Changing alphabet String to alphabet Array
alphArr = alphStr.toCharArray();
for(int i = 0; i < 26; i++) {
intArr[i] = i;
}
}
public static ArrayList<Integer> cIntToArrL() {
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < 26; i++) {
list.add(i);
}
return list;
}
public static int getNumericValue(char c) {
return Character.getNumericValue(c) - 10;
}
public static char getCharValue(int i) {
return Character.forDigit(i + 10, i + 11);
}
}
我被你的密码迷住了。如果我理解了您想要正确完成的任务,那么您需要随机生成2D方形数组,其中每一行和每一列中的每个字符都是完全相同的,不重复,也不从字母表中丢失。是这样吗?如果是,以下是我解决这个问题的方法: 使用独特的字符规则从字母表创建图案 随机洗牌所有行,可能使用种子 随机洗牌所有列,可能使用种子 步骤1-创建表: 步骤3-洗牌列: 就这样。我希望有帮助
char alphabet[] = {'a','b','c', ...}; // Set of chars that will be used in table
char table[][] = new char[26][26]; // Size must be same as size of alphabet
private void prepareTable() {
for (int x = 0; x < table.length; x++) {
for (int y = 0; y < table[x].length; y++) {
// Copy alphabet to row in table but offset it each row by 1
table[x][y] = alphabet[(x + y) % alphabet.length];
}
}
// Now table looks like this:
// 'a' 'b' 'c'
// 'b' 'c' 'a'
// 'c' 'a' 'b
}
Random r = new Random( /* seed */ );
void shuffleRows() {
int items = table.length;
while (items > 0) { // While there is something to shuffle
int index = r.nextInt(items);
// Simple swap
char[] tmp = table[index];
table[index] = table[items - 1];
table[items - 1] = tmp;
items --; // Move on to the next
}
}
void shuffleColumns() {
int items = table[0].length;
while (items > 0) {
int index = r.nextInt(items);
// Swap chars in each row
for (int i = 0; i < table.length; i++) {
char tmp = table[i][index];
table[i][index] = table[i][items - 1];
table[i][items - 1] = tmp;
}
items --;
}
}