“变量”无法解析为变量

我正在学习一个教程,我是新手,我需要帮助来解决这个问题。如果有人可以解释为什么会出现此错误以及如何解决它。


import java.util.*;

public class bucky {


    public static void main(String[] args) {


        String[] things = {"apples", "noobs", "pwnge", "bacon", "goATS"};

        List<String> list1 = new java.util.LinkedList<String>();

        for(String x : things)

            list1.add(x);


        String[] things2 = {"sausage", "bacon", "goats", "harrypotter"};

        List<String> list2 = new java.util.LinkedList<String>();

        for(String y : things2)

            list2.add(y);


        list1.addAll(list2);

        list2 = null;


        printMe(list1);

        removeStuff(list1, 2,5);

        printMe(list1);

        reverseMe(list1);



        }


    //printMe method

    private static void printMe(List<String> l) {

        for (String b : l);

            System.out.printf("%s ", b); //This is the line where I get the error

        System.out.println();

    }


    //removeStuff method

    private static void removeStuff(List<String> l, int from, int to) {

        l.subList(from, to).clear();

    }


    //reverseMe method

    private static void reverseMe(List<String> l) {

        ListIterator<String> bobby = l.listIterator(l.size());

        while(bobby.hasPrevious())

            System.out.printf("%s ", bobby.previous());

    }

}


PIPIONE
浏览 274回答 3
3回答

慕森卡

去掉分号for&nbsp;(String&nbsp;b&nbsp;:&nbsp;l);

米脂

您的方法 printMe 有语法错误://printMe methodprivate static void printMe(List<String> l) {&nbsp; &nbsp; for (String b : l);&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%s ", b);&nbsp; &nbsp; System.out.println();}消除 ; 在 for 循环之后并替换为 {} 参考这里:&nbsp;//printMe methodprivate static void printMe(List<String> l) {&nbsp; &nbsp; for (String b : l) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%s ", b);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }}

慕容3067478

从printMe方法中删除分号-://打印我方法private static void printMe(List<String> l) {&nbsp; &nbsp; for (String b : l)&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%s ", b);&nbsp; &nbsp; System.out.println();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java