Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Creación de una tabla de caracteres 2D con caracteres únicos en cada línea y columna

Estoy tratando de hacer una tabla 2D en la que los caracteres no se pueden repetir en columna o línea. Estoy tratando de hacer al menos 2 líneas de matrices. Puedo hacer la primera, pero tengo un problema con otras líneas. En la segunda línea arroja un error - ArrayIndexOutOfBoundsException -11. ¿Alguien me puede ayudar?

Clase principal:

 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]); } }

La clase 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); } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Me perdí un poco en tu código. Si entiendo lo que quiere lograr correctamente, desea generar aleatoriamente una matriz 2D (cuadrada) donde en cada fila y columna cada carácter es exactamente una vez (no duplicado y no falta en el alfabeto). ¿Está bien? En caso afirmativo, aquí está mi enfoque sobre cómo abordar este problema:

  1. Crear patrón a partir del alfabeto (con esa regla de caracteres únicos)
  2. Mezcla todas las filas (al azar, posiblemente usando semillas)
  3. Mezclar todas las columnas (al azar, posiblemente usando semilla)

Paso 1 - crear tabla:

 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 }

Paso 2: barajar filas: Para barajar real, estoy usando 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 } }

Paso 3 - columnas aleatorias:

 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 --; } }

Y eso es. Espero que ayude.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!