在 Java 中设置字符数组的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3949572/
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
Set size on char array in Java
提问by VansFannel
I'm developing an Android application.
我正在开发一个 Android 应用程序。
I want to set size to a char array like this:
我想将大小设置为这样的字符数组:
public char[5] language;
But it doesn't work. I have to delete number five to make it work.
I want to limit to five characters to language variable. How can I do that?
但它不起作用。我必须删除第五个才能使它工作。
我想将语言变量限制为五个字符。我怎样才能做到这一点?
Thanks.
谢谢。
采纳答案by Stephen C
You cannot do it like that.In Java, the type of an array does not include it's size. See my answer to this earlier question. (Ignore the part about abstract
methods in that question ... it's not the real issue.)
你不能那样做。在 Java 中,数组的类型不包括它的大小。请参阅我对早先问题的回答。(忽略该abstract
问题中有关方法的部分……这不是真正的问题。)
The size of an array is determined by the expression that creates it; e.g. the following creates a char array that contains 5 characters, then later replaces it with another array that contains 21 characters.
数组的大小由创建它的表达式决定;例如,下面创建一个包含 5 个字符的 char 数组,然后用另一个包含 21 个字符的数组替换它。
public char[] language = new char[5];
...
language = new char[21];
Note that the creation is done by the expression on the RHS of the equals. The length of an array is part of its 'value', not its 'type'.
请注意,创建是通过等号的 RHS 上的表达式完成的。数组的长度是其“值”的一部分,而不是其“类型”。
回答by Colin Hebert
To quote the JLS :
引用 JLS :
An array's length is not part of its type.
数组的长度不是其类型的一部分。
To initialize an array you should do :
要初始化数组,您应该执行以下操作:
public char[] language = new char[5];
Other solutions are
其他解决方案是
public char[] language = {0, 0, 0, 0, 0};
or
或者
public char[] language;
language = new char[5];
In Java, the array declaration can't contain the size of the array; we only know the variable will contain an array of a specific type. To have an array initialized (and with a size) you have to initialize it either by using new
or by using a shortcut which allows to initialize and set values for an array at the same time.
在 Java 中,数组声明不能包含数组的大小;我们只知道变量将包含一个特定类型的数组。要初始化数组(并具有大小),您必须通过使用new
或使用允许同时初始化和设置数组值的快捷方式来初始化它。
Best way to have to check if an array has a specified size, is actually checking the size of the array yourself with something like if(array.length == 5)
.
必须检查数组是否具有指定大小的最佳方法实际上是使用类似if(array.length == 5)
.
Resources :
资源 :
On the same topic :
在同一主题上:
回答by andersoj
public char[] language = new char[5];
You can't limit the variable itself to length 5; you need to enforce that invariant in your logic. Which means it also shouldn't be public:
您不能将变量本身限制为长度为 5;您需要在逻辑中强制执行该不变量。这意味着它也不应该是公开的:
private char[] language = new char[5];
...
public void setLanguage(final char[] language)
{
// or maybe language.length != 5, or whatever you really mean
if (language.length > 5)
throw new IllegalArgumentException("language must have length <= 5");
this.language = language;
}
回答by RonU
The variable itself is just of type char-array (char[]
), so you can't limit the size of the variable type. What you can limit is the size of the array you instantiate and save to that variable:
变量本身只是 char-array ( char[]
)类型,因此您不能限制变量type的大小。您可以限制的是您实例化并保存到该变量的数组的大小:
char[] language = new char[4];
回答by Oscar Kurniawan Manule
Maybe you want to try the generic style by using ArrayList as the representation of array. So you may find flexibility using ArrayList methods.
也许您想通过使用 ArrayList 作为数组的表示来尝试通用风格。因此,您可能会发现使用 ArrayList 方法的灵活性。
private ArrayList<Character> alchar = new ArrayList<Character>(5);
............................................
public void addChar(Character ch){
if (alchar.size() <= 5)
alchar.add(ch);
}
public ArrayList<Character> getChars(){
return alchar;
}