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();
}
}
3 个解决方案
#1
15
int
is 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])
#2
5
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,
- Java编码约定,
- limited scope for the variable
i
, - 变量i的范围有限,
- we do something with the
String[]
, - 我们用String []做一些事情,
- give the method a meaningful name,
- 给这个方法一个有意义的名字,
- try to use consist formatting.
- 尝试使用编组格式。
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 String
you 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);
}
#3
4
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:
您的代码尝试调用int值的toString()方法。在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)比我的系统上的Integer.toString(int)快一点(Java 1.7)。
#1
15
int
is 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])
#2
5
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,
- Java编码约定,
- limited scope for the variable
i
, - 变量i的范围有限,
- we do something with the
String[]
, - 我们用String []做一些事情,
- give the method a meaningful name,
- 给这个方法一个有意义的名字,
- try to use consist formatting.
- 尝试使用编组格式。
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 String
you 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);
}
#3
4
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:
您的代码尝试调用int值的toString()方法。在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)比我的系统上的Integer.toString(int)快一点(Java 1.7)。