简体   繁体   English

未使用局部变量类别的值

[英]The value of the local variable category is not used

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    String category[] = request.getParameterValues("category");
}

String Category[] line displays: String Category[]行显示:

"The value of the local variable category is not used" “未使用局部变量类别的值”

Secondly, I want to pass this array to my controller class to set as its getters/setter:其次,我想将此数组传递给我的 controller class 设置为它的 getter/setter:

    public class Controller {
        private String[] category;
    
        public Controller(String[] category) {
            super();
            this.category[] = category;
        }
    }

This throws error:这会引发错误:

"Type mismatch: cannot convert from String[] to String" “类型不匹配:无法从 String[] 转换为 String”

I am beginner.我是初学者。 Please advise how can I fix these two errors/warnings请告知我该如何解决这两个错误/警告

Best regards此致

That is because you are trying to set an array, which is technically a reference object, to another array(another reference object).那是因为您试图将一个数组(技术上是参考 object)设置为另一个数组(另一个参考对象)。 When you are defining your arrays you will need to have the square brackets but when trying to call on the variable you won't always need it.当您定义 arrays 时,您需要使用方括号,但在尝试调用变量时,您并不总是需要它。

this.category[] = category;

needs to become需要成为

this.category = category;

This line will give you what you want and define category.此行将为您提供所需的内容并定义类别。 I know you said you were a beginner but I'd try looking into ArrayLists because they will make your life a lot easier in things like this.我知道你说你是一个初学者,但我会尝试研究 ArrayLists,因为它们会让你在这样的事情上变得更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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