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

231
Views
¿Cuál es el tipo de argumento para un método que toma matrices anidadas indefinidas?

Estoy tratando de construir un método estático que atraviese una matriz anidada de matrices. Me gustaría establecer este argumento de método para aceptar cualquier número de matrices anidadas. En otras palabras, esta función debería poder operar una matriz de matrices o una matriz de matrices de matrices (por ejemplo).

Ilustro aquí con un ejemplo:

 private static void doStuffWithArrays(someType arrays){ //Do some stuff }

¿Cuál es el tipo de datos correcto para estar en lugar de someType ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Deberías usar Object[] .

Los métodos en el JDK que toman una matriz anidada arbitrariamente, como deepToString , también hacen esto.

Dado que no sabe si un objeto en esa matriz más externa es una matriz interna, debe verificar getClass().isArray() :

 private static void doStuffWithArrays(Object[] outermostArray){ for (int i = 0 ; i < outermostArray.length ; i++) { Object outerElement = outermostArray[i]; if (outerElement.getClass().isArray()) { Object[] innerArray = (Object[])outermostArray[i]; // ... do things with innerArray } else { // we reached the innermost level, there are no inner arrays } } }

Sin embargo, si está tratando con matrices de primitivas, deberá verificar cada una de las clases de matrices primitivas por separado y luego convertirlas en la correcta.

 if (outerElement.getClass() == int[].class) { int[] innerArray = (int[])outermostArray[i]; // ... do things with innerArray } else if (outerElement.getClass() == short[].class) { short[] innerArray = (short[])outermostArray[i]; // ... do things with innerArray } else if (outerElement.getClass() == long[].class) { long[] innerArray = (long[])outermostArray[i]; // ... do things with innerArray } else if ... // do this for all the other primitive array types } else if (outerElement.getClass().isArray()) { Object[] innerArray = (Object[])outermostArray[i]; // ... do things with innerArray } else { // we reached the innermost level, there are no inner arrays }

deepToString también hace esto.

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!