I have the following code and it turns out that when I run it, it executes first for (ListIterator it = pila1.listIterator(pila1.size()); it.hasPrevious();)
, then for (ListIterator itt = pila2.listIterator(pila2.size()); itt.hasPrevious();)
and then the rest of the code, and then returns to for (ListIterator it = pila1.listIterator(pila1.size()); it.hasPrevious();)
, that is, it does not finish going through all the elements of the second cycle for
, why?
Notes: The structure is for(alguna_declaracion; cuantas_veces_se_ejecutara;)
. The program works fine :)
public class ClaseEjercicio31ago {
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
List<Integer> pila1 = new ArrayList<Integer>();
List<Integer> pila2 = new ArrayList<Integer>();
List<Integer> cola = new ArrayList<Integer>();
System.out.println("Introduzca los digitos (5) para la pila1");
for (char digito : leer.nextLine().toCharArray()) {
pila1.add(digito - '0');
}
System.out.println("Introduzca los digitos (5) para la pila2");
for (char digito : leer.nextLine().toCharArray()) {
pila2.add(digito - '0');
}
int uno = 0;
for (ListIterator it = pila1.listIterator(pila1.size()); it.hasPrevious();) {
for (ListIterator itt = pila2.listIterator(pila2.size()); itt.hasPrevious();) {
int n1 = (Integer) it.previous();
int n2 = (Integer) itt.previous();
int n3 = (n1 + n2 + uno);
if (n3 >= 10) {
cola.add(n3 - 10);
uno = 1;
} else {
cola.add(n3 + uno);
}
it.remove();
itt.remove();
}
}
if (uno == 1) {
cola.add(1);
}
System.out.println("La suma de las pilas es:");
for (Iterator it = cola.iterator(); it.hasNext();) {
int t = (Integer) it.next();
System.out.print("\n " + t + "\n");
it.remove();
}
The type of for that you use is (if I don't remember) the most basic there is and the first one, which reads as follows:
An inconvenience with this for is that, if you do not define the condition with which to iterate well, you may not go through the array completely or you may get an Exception due to an index outside the array.
Instead of it, to loop through objects I recommend you use this form of for:
By having the variable o of type Object it will iterate all the objects inside the array. I hope my answer has helped and served you. Greetings.
In the first iteration, you start with two lists of 5 elements.
You enter the
for
outer, you get an iterator at the end 1 of the first list, ashasPrevious
istrue
you enter the loop.You go into the
for
internet, you get an iterator at the end 1 of the second list, ashasPrevious
it istrue
you go into the loop.On each iteration of the inner loop, you get the previous element from both lists, and remove that element from each list . That is, in the first iteration of the outer loop you are going to do 5 iterations of both lists and remove 5 elements from both lists , leaving both lists empty and the two iterators at the beginning of their respective lists.
From that point on, both
it.hasPrevious()
returnitt.hasPrevious()
false so both are exitedfor
.As for the solution, it is not clear what the code has to do so there can be no answer, but what is clear is that in the current code one of them is
for
left over.1 "At end" means "after last element", so first
previous
returns the last element.You should explain what your code should do
Anyway, it seems to me that your code receives two numbers, stores them in two lists and then adds them digit by digit.
Loop Behavior
The problem you have is that you do n't need two loops .
If you want to add two lists element by element, you have to do a number of loops exactly equal to the size of the larger list , which is solved by iterating through that list. What you want to do is iterate through both lists, but do it a number of times equal to the size of the other list. That is, You have many cycles left over .
In mathematical terms, we can say that yes
n = m = lista2.size();
, you want to don * m
loops when really you just needn
loops.I've taken the liberty of modifying your code. The code inside the loops looks like this:
You will notice that I have modified your
else
, I think this is the behavior it should have.code improvement
As for code improvement, I can give you a couple of rules of thumb that will improve how well your code can be read, tested, and modified.
main
, it's hard to read and test. If you separate it into small methods that deal with a single thing and, in addition, name those methods in an appropriate way (Explaining what they do) you will be able to better understand what happens in your code.Referring to your code specifically, I can tell you:
Character.getNumericValue()
is easier to read thandigito - '0'
. It also protects you against non-Unicode numbers.List<Integer> pila1 = new ArrayList<Integer>();
it is the same asList<Integer> pila1 = new ArrayList<>();
Iterator
seems too much to me to iterate a list and operate two. I think afor
normal loop would have served you better.Streams
, Lambda Expressions , the Interface,Iterable
and Method References .I have rewritten your code following the criteria I have listed:
Incidentally, adding the lists could probably have been fixed in a line or two of code with Streams and its
map()
and methodsfilter()
. I'll try to do it later if required.