-2

Array index elements are well defined outside a for cicle, once in, they recieve the Null value.

I made a class called Auto which has 5 attributes on its consutructor. I want to make a list in which I will have N (3 in this code) objects of the previously mentioned class with their attributes created randomly. The issue arises when I want to add an if condition to stop objects with an already used value for the attribute "placa" being added to the list. When in the for cycle the array made to store all the "placa" values added becomes Null for all its values.

import java.util.ArrayList;
import java.lang.Math;

public class GenLista {

public void genLista() {

    int max = 3;

    ArrayList<Auto> c = new ArrayList<Auto>();
    String color[] = new String[5];
    color[0] = "Rojo"; color[1] = "Verde"; color[2] = "Azul"; color[3] = "Negro"; color[4] = "Blanco";
    String placas[] = new String[3];
    placas[0] = "JCX"; placas[1] = "HTT"; placas[2] = "CDX";

    for(int mx = 0 ; mx<max ; mx++) {

        String col = color[(int) Math.round(Math.random()*4)];
        int cap = (int) Math.round(Math.random()*10+40);
        double kilom = (Math.round(Math.random()*100000)*100.00)/100;
        String placa = placas[(int) Math.round(Math.random()*0)] + "-" + (int) Math.round(Math.random()*9);// + (int) Math.round(Math.random()*9)+ "-" + (int) Math.round(Math.random()*9) + (int) Math.round(Math.random()*9);
        double precio = (Math.round(Math.random()*100000+200000)*100.00)/100;

        Auto z = new Auto(col, cap, kilom, placa, precio);

        String plac[] = new String[max];
        System.out.println("mx = " + mx);
        plac[mx]=placa;
        System.out.println("plac" + mx + " = " + plac[mx]);
        boolean sats = false;           

        if (mx != 0) {

            for(int k=0 ; k<mx; k++) {
                System.out.println(k + "," + mx);
                System.out.println("lista plac" + k + " = " + plac[k]);

            }
        }
        else if( mx == 0) {
            sats = true;
        }

        if(sats) {
            c.add(z);
        }

        //System.out.println(z.getPlaca());
        //System.out.println(c);
    }
}
}

The exit is:

mx = 0
plac0 = JCX-3
mx = 1
plac1 = JCX-5
0,1
lista plac0 = null
mx = 2
plac2 = JCX-8
0,2
lista plac0 = null
1,2
lista plac1 = null
mx = 3

In the exit you can notice that the array plac should have JCX-5 in its 0 index, as shown in the print "plac0 = JCV-5", but when in the for, the result is Null, as one can see in the print "lista plac0 = null".

  • Loop should be `for (int mx = 0; mx < max; mx++)` – Andreas Aug 24 '19 at 17:47
  • You should learn to use array initializers, e.g. `String[] placas = { "JCX", "HTT", "CDX" };` – Andreas Aug 24 '19 at 17:48
  • It does stop errors, but it continues te resolve as null inside the for. I will look into initializers :) thanks – Kalio O'Farril Aug 24 '19 at 17:50
  • mx = 0 plac0 = JCX-5 mx = 1 plac1 = JCX-8 0,1 lista plac0 = null mx = 2 plac2 = JCX-7 0,2 lista plac0 = null 1,2 lista plac1 = null – Kalio O'Farril Aug 24 '19 at 17:52
  • You might also want to think about making a [small helper](https://stackoverflow.com/questions/8065532/how-to-randomly-pick-an-element-from-an-array) to grab a random element from an array, – Michael Aug 24 '19 at 18:01

2 Answers2

1

Index out of bounds

Arrays are zero-indexed. That means that the number to reference each slot in array starts counting at zero. Thus an array arr of size three will contain three slots, referenced by the numbers arr[0], arr[1], and arr[2].

The for loop you have written will run the body of code when the conditional written is true. Here, you have an array of max size 3. The conditional makes mx iterate up to any number less than or including 3. So, on the last iteration, mx is 3, and you try to grab the 3rd slot, but as established above, the 2nd slot is the highest you can grab. Simply change the conditional as such:

for(int mx = 0; mx < max ; mx++) {
    // ...
}

Null issue

In each outer iteration of your loop, you declare a brand new array:

String plac[] = new String[max];

This means that all the previous elements of plac that you modified in other iterations are lost. So, to fix this issue, simpy move this code outside of the loop.

Michael
  • 2,673
  • 1
  • 16
  • 27
1

When declaring the below array, add one to max

String plac[] = new String[max + 1];

Why?

Well, your variable max(3) is the length of the array. Therefore only index 0 to 2 will work unless you add one to the max.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
Christopher
  • 425
  • 3
  • 9