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

241
Views
elemento adicional agregado en mi matriz int por mi ciclo for, no entiendo por qué

así que estoy haciendo un pequeño ejercicio llamado 'matriz de múltiplos', en el que estoy multiplicando un número y mostrando todos los múltiplos del número hasta que una matriz llega a la respuesta.

por ejemplo: arrayOfMultiples(7, 5) ➞ [7, 14, 21, 28, 35].

Mi problema es que tengo un elemento adicional que se agrega a mi matriz con un valor de 0, y no entiendo por qué. ¡He tratado de entender por qué, pero necesito ayuda!

este es mi código:

 public static void main(String[] args) { //invoke method int[] test = arrayOfMultiples(7, 6); //convert array to string to print entire thing System.out.println(Arrays.toString(test)); } public static int[] arrayOfMultiples(int num, int length) { //array is initalised with original number int [] array = new int [num]; //variable to keep num the same int add = num; // loop for adding the new multiples to the array for (int i = 0; i < length; i++) { array[i] = num; num += add; } return array; }

mi salida es: [7, 14, 21, 28, 35, 42, 0]

¡gracias!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Su problema es que para la inicialización de la matriz, utilizó la variable num en lugar de la longitud para especificar el tamaño de la matriz y, dado que int es un tipo de datos premitivo, no puede ser nulo, sino que tiene un valor predeterminado de 0. Pruebe esto:

 public static void main(String[] args) { //invoke method int[] test = arrayOfMultiples(7, 6); //convert array to string to print entire thing System.out.println(Arrays.toString(test)); } public static int[] arrayOfMultiples(int num, int length) { //array is initalised with original number int [] array = new int [length]; //variable to keep num the same int add = num; // loop for adding the new multiples to the array for (int i = 0; i < length; i++) { array[i] = num; num += add; } return array; }
over 4 years ago · Santiago Trujillo Report

0

El problema es que estás haciendo que el tamaño de la matriz sea el número para el que tienes que encontrar los múltiplos. Debe hacer que el tamaño sea igual a int length . Imagínese si num = 1000 y length = 10 , es decir, desea encontrar 10 múltiplos de 1000 . La forma en que lo ha hecho es que obtendrá una matriz con 990 elementos como 0 , que es el valor predeterminado asignado en cada índice en un int [] .

Reemplazar

 int [] array = new int [num];

con

 int [] array = new int [length];
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!