我正在尝试制作一个二维表,其中字符不能在列或行中重复。我正在尝试做至少 2 行数组。我可以做第一个,但我对其他行有问题。在第二行,它抛出一个错误 - ArrayIndexOutOfBoundsException -11。有人能帮我吗?
主要类:
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);
}
}
我有点迷失在你的代码中。如果我理解你想正确完成什么,你想随机生成二维(正方形)数组,其中每一行和每一列的每个字符都恰好一次(不重复,也不在字母表中丢失)。是对的吗?如果是,这是我如何解决这个问题的方法:
第 1 步 - 创建表:
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
}
第 2 步 - 洗牌行:对于实际洗牌,我使用Fisher-Yates Shuffle
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
}
}
第 3 步 - 洗牌列:
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 --;
}
}
就是这样。我希望它有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句