Error of int cannot be dereferenced?

后端 未结 3 1446
面向向阳花
面向向阳花 2021-01-06 19:51

I am getting an error with this constructor, and i have no idea how to fix? I am a beginner at java. This is from an example exercise that i was trying to learn:

<         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 20:27

    You code tries to call the toString() method of an int value. In Java, int is a primitive type and has no methods. Change the line:

    Array[i] = intArray[i].toString();
    

    to

    Array[i] = String.valueOf(intArray[i]);
    

    and the code should run. By the way, you should use lowerCamelCase for variables and fields.

    Edit: For what it's worth, String.valueOf(int) is a bit faster than Integer.toString(int) on my system (Java 1.7).

提交回复
热议问题