The source of this is, of course, poor code design. Also, why do you initialize a variable if the arraylist does not contain a field (you use it later)? A larger example or what exactly you are trying to do will help here. Usually just using
But I can at least two ways to do this:
switch(myArrayList.size()) { case 5: var4 = myArrayList.get(4); case 4: var3 = myArrayList.get(3); case 3: var2 = myArrayList.get(2);
or just use try / catch.
try { var0 = myArrayList.get(0); var1 = myArrayList.get(1); } catch(IndexOutOfBoundsException ex){ }
But, of course, it is better to use the arraylist itself and just use it with the default values ββthat you would use for your variables.
Voo source share