Pergunta

I'm new here and it's first time when I post, so please excuse me if I'll make any mistakes. I want to display elements from 4 LinkedList and those elements to be displayed like in a table. I don't need JTable because i don't need GUI. I've tried with ListIterator but it doesn't show them all: I mean if a list has 5 elements and another have 3 will be shown only 3 elements from each list. A piece of code worth more than a milion words, so :

    LinkedList list1 = new LinkedList();
    LinkedList list2 = new LinkedList();
    LinkedList list3 = new LinkedList();
    LinkedList list4 = new LinkedList();

    list1.addFirst("A");
    list1.addFirst("B");
    list1.addFirst("C");
    list1.addFirst("D");
    list1.addFirst("E");
    list1.addFirst("F");

    list2.addFirst("G");
    list2.addFirst("H");
    list2.addFirst("I");
    list2.addFirst("J");
    list2.addFirst("K");
    list2.addFirst("L");
    list2.addFirst("M");

    list3.addFirst("N");
    list3.addFirst("O");
    list3.addFirst("P");
    list3.addFirst("Q");
    list3.addFirst("R");
    list3.addFirst("S");
    list3.addFirst("T");
    list3.addFirst("U");

    list4.addFirst("V");
    list4.addFirst("W");
    list4.addFirst("X");
    list4.addFirst("Y");
    list4.addFirst("Z");

    ListIterator it1 = list1.listIterator();
    ListIterator it2 = list2.listIterator();
    ListIterator it3 = list3.listIterator();
    ListIterator it4 = list4.listIterator();

    while (it1.hasNext() && it2.hasNext() && it3.hasNext() && it4.hasNext()){

        System.out.println(it1.next()+"\t\t\t"+ it2.next()+"\t\t\t"+it3.next()+"\t\t\t"+it4.next());
    }

And the output is like this :

http://imageshack.us/photo/my-images/9/capturelkt.png/

I am confused why is this the output, so if someone can explain me what I'm doing wrong or can give me a hint or a example, I'll be gratefull. This is not a homework, I'm just kinda learning JAVA alone. Thank you in advance !

Foi útil?

Solução

The reason you don't see the row with A in it is because of this line:

it1.hasNext() && it2.hasNext() && it3.hasNext() && it4.hasNext()

As soon as one of your lists is out of elements, you stop.

To fix it you have to continue to work until ALL your lists are out of elements. But, you can't just access those lists if they're out of elements, so you have to work around it.

// change && to ||
while(it1.hasNext() || it2.hasNext() || it3.hasNext() || it4.hasNext()) {
    Object[] objs = new String[] { "", "", "", ""}; // empty strings
    // if iterators on this row are not empty, replace with values
    if(it1.hasNext()) objs[0] = it1.next();
    if(it2.hasNext()) objs[1] = it2.next();
    if(it3.hasNext()) objs[2] = it3.next();
    if(it4.hasNext()) objs[3] = it4.next();
    // now print
    String tabs = "\t\t\t";
    System.out.println(objs[0] + tabs + objs[1] + tabs + objs[2] + tabs + objs[3]);

}

This would be faster without the array, and instead used a strict number of objects. But I see it as cleaner and easier to refactor if we leave it as an array.

Outras dicas

it prints from the start so as you are printing next(iterating through it) you will print the most recently added item, because you are adding items to teh start each time)

with this line

while (it1.hasNext() && it2.hasNext() && it3.hasNext() && it4.hasNext()){

you are also only printing only if all lists have a next value, only 5 values are printed because your shortest list is only has a size of 5(list 4)

You're using && so the moment an iterator's hasNext() returns false, the entire evaluation of (it1.hasNext() && it2.hasNext() && it3.hasNext() && it4.hasNext()) will be false.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top