Java int 错误不能解除引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28213083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:48:25  来源:igfitidea点击:

Error of int cannot be dereferenced?

javaarrays

提问by Hotfin

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:

这个构造函数出现错误,我不知道如何解决?我是java初学者。这是来自我试图学习的示例练习:

/** 
 * Create an array of size n and store a copy of the contents of the
 * input argument
 * @param intArray array of elements to copy 
*/
public IntArray11(int[] intArray)
{
    int i = 0;
    String [] Array = new String[intArray.length];
    for(i=0; i<intArray.length; ++i)
    {
    Array[i] = intArray[i].toString();
    }
}

回答by amit

intis not an object in java (it's a primitive), so you cannot invoke methods on it.

int不是 java 中的对象(它是一个原语),所以你不能调用它的方法。

One simple way to solve it is using Integer.toString(intArray[i])

解决它的一种简单方法是使用 Integer.toString(intArray[i])

回答by llogiq

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

您的代码尝试调用值的 toString() 方法int。在 Java 中,int是原始类型并且没有方法。更改行:

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.

并且代码应该运行。顺便说一句,您应该对变量和字段使用lowerCamelCase。

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

编辑:对于它的价值, String.valueOf(int) 在我的系统(Java 1.7)上比 Integer.toString(int) 快一点。

回答by Peter Lawrey

I would write it more like this

我会更像这样写

public String[] convertToStrings(int... ints) {
    String[] ret = new String[ints.length];
    for(int i = 0; i < intArray.length; ++i)
        ret[i] = "" + ints[i];
    return ret;
}

Or in Java 8 you might write

或者在 Java 8 中你可能会写

public List<String> convertToStrings(int... ints) {
    return IntStream.of(ints).mapToObj(Integer::toString).collect(toList());
}

This uses;

这使用;

  • Java coding conventions,
  • limited scope for the variable i,
  • we do something with the String[],
  • give the method a meaningful name,
  • try to use consist formatting.
  • Java编码约定,
  • 变量的范围有限i
  • 我们做一些事情String[]
  • 给方法一个有意义的名字,
  • 尝试使用组合格式。

If we were worried about efficiency it is likely we could do away with the method entirely.

如果我们担心效率,很可能我们可以完全取消该方法。

String.valueOf(int)is not faster than Integer.toString(int). From the code in Stringyou can see that the String implementation just calls Integer.toString

String.valueOf(int)不比 快Integer.toString(int)。从里面的代码String可以看出,String的实现只是调用了Integer.toString

/**
 * Returns the string representation of the {@code int} argument.
 * <p>
 * The representation is exactly the one returned by the
 * {@code Integer.toString} method of one argument.
 *
 * @param   i   an {@code int}.
 * @return  a string representation of the {@code int} argument.
 * @see     java.lang.Integer#toString(int, int)
 */
public static String valueOf(int i) {
    return Integer.toString(i);
}