Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
extra element being added in my int array by my for loop, I don't understand why

so I'm doing a small exercise called 'array of multiples', where I am multiplying a number and displaying all the multiples of the number until the answer is reached by an array.

for example: arrayOfMultiples(7, 5) ➞ [7, 14, 21, 28, 35].

My issue is that I have an additional element being added to my array with a value of 0, and I do not understand why. I've tried to understand why but I need help!

this is my code:

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

my output is: [7, 14, 21, 28, 35, 42, 0]

thank you!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Your issue is that for the initialization of the array you used the num variable instead of length to specify the size of the array and since int is a premitive data type it cannot be null, rather it has a default value of 0. Try this:

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 Denunciar

0

The problem is that you are making the size of the array as the number for which you have to find the multiples. You need to make the size equal to int length. Just imagine if num = 1000 and length = 10 i.e. you want to find 10 multiples of 1000. The way you have done is, you will get an array with 990 elements as 0 which is the default value assigned at each index in an int [].

Replace

int [] array = new int [num];

with

int [] array = new int [length];
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda