简体   繁体   中英

ArrayList isn't printing out

I have created two arrays. I added them up. Then I wanted to print out the new array by defining a method. But the method can't print out the array. Where is the error in my code?

package stringpractice;

import java.util.LinkedList;
import java.util.List;

public class StringPractice {

    public static void main(String[] args) {

        String[] boy = {"John", "Russel", "Ryan"};
        List<String> l1 = new LinkedList<String>();

        for (String x : l1) {
            l1.add(x);

        }

        String[] girl = {"Sara", "Leena", "Emilia"};
        List<String> l2 = new LinkedList<String>();
        for (String y : l2) {

            l2.add(y);
            l1.addAll(l2);
            l2 = null;
            printMe(l1);

            /*removeStuff(l1, 1,2);
             reverseMe(l1);
             */
        }

    }

    public static void printMe(List<String> l1) {
        for (String p : l1) {
            System.out.printf("%s ", p);
        }
        System.out.println();

    }

}

There is nothing to print in your list. You need to change

for (String x : l1) // l1 don't have a single element
                    // at this moment 

and

for (String y : l2) // same as l1 

To

for (String x : boy)

and

for (String x : girl) 

Again

 l2.add(y);
 l1.addAll(l2);
 l2 = null; // inside for lopp l2 become null

So you will get NPE from here.

Corrected code

  public static void main(String[] args) {
    String[] boy = {"John", "Russel", "Ryan"};
    List<String> l1 = new LinkedList<>();
    for (String x : boy) {
        l1.add(x);
    }
    String[] girl = {"Sara", "Leena", "Emilia"};
    List<String> l2 = new LinkedList<>();
    for (String y : girl) {
        l2.add(y);
        l1.addAll(l2);
        printMe(l1);
    }
    l2 = null;
}

public static void printMe(List<String> l1) {
    for (String p : l1) {
        System.out.printf("%s ", p);
    }
    System.out.println();
}

The for-loops walk through the still empty lists.

   for (String x : boys) {
        l1.add(x);
   }

Or

   Collections.addAll(l1, boys);

Setting l2 to null will not loop well. And the addAll can be done outside the loop.

your both the List L1 and L2 are empty. so your both the for loop body is not executed.

public class StringPractice { public static void main(String[] args) {

    String[] boy = { "John", "Russel", "Ryan" };
    List<String> l1 = new ArrayList<String>(Arrays.asList(boy));

    String[] girl = { "Sara", "Leena", "Emilia" };
    List<String> l2 = new ArrayList<String>(Arrays.asList(girl));

    l1.addAll(l2);
    l2 = null;
    printMe(l1);

    /*removeStuff(l1, 1,2);
     reverseMe(l1);
     */

}

public static void printMe(List<String> l1) {
    for (String p : l1) {
        System.out.printf("%s ", p);
    }
    System.out.println();

}

You arent traversing the boy[] and girl[] array anywhere. You need to iterate like for (String x : boy) for some result.

您需要遍历boy []和girl []数组以在其中打印内容

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM