我想在 java 中声明一个空数组,然后我想更新它但代码不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34859322/
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
I want to declare an empty array in java and then I want do update it but the code is not working
提问by Saif Arsalan
I want to declare an empty array in java and then I want do update it but the code is not working...
我想在java中声明一个空数组,然后我想更新它但代码不起作用......
public class JavaConversion
{
public static void main(String args[])
{
int array[]={};
int number = 5, i = 0,j = 0;
while (i<4) {
array[i]=number;
i=i+1;
}
while (j<4) {
System.out.println(array[j]);
}
}
}
回答by brso05
You need to give the array a size:
你需要给数组一个大小:
public static void main(String args[])
{
int array[] = new int[4];
int number = 5, i = 0,j = 0;
while (i<4){
array[i]=number;
i=i+1;
}
while (j<4){
System.out.println(array[j]);
j++;
}
}
回答by Ian Grainger
You can't set a number in an arbitrary place in the array without telling the array how big it needs to be. For your example: int[] array = new int[4];
你不能在不告诉数组它需要多大的情况下在数组中的任意位置设置一个数字。对于您的示例:int[] array = new int[4];
回答by Ross Drew
You are creating an array of zero length (empty)
您正在创建一个长度为零(空)的数组
int array[]={/*nothing in here = array with no elements*/};
and then trying to assign values to array elements (which you don't have, because it's empty)
然后尝试为数组元素赋值(你没有,因为它是空的)
array[i] = number; //array[i] = element i in the array
You need to define a larger array to fit your needs
您需要定义一个更大的数组来满足您的需求
int array[] = new int[4]; //Create an array with 4 elements [0],[1],[2] and [3] each containing an int value
回答by Mage Xy
Your code compiles just fine. However, your array initialization line is wrong:
你的代码编译得很好。但是,您的数组初始化行是错误的:
int array[]={};
What this does is declare an array with a size equal to the number of elements in the brackets. Since there is nothing in the brackets, you're saying the size of the array is 0 - this renders the array completely useless, since now it can't store anything.
这样做是声明一个数组,其大小等于括号中的元素数。由于括号中没有任何内容,您是说数组的大小为 0 - 这使数组完全无用,因为现在它无法存储任何内容。
Instead, you can either initialize the array right in your original line:
相反,您可以直接在原始行中初始化数组:
int array[] = { 5, 5, 5, 5 };
Or you can declare the size and then populate it:
或者您可以声明大小然后填充它:
int array[] = new int[4];
// ...while loop
If you don't know the size of the array ahead of time (for example, if you're reading a file and storing the contents), you should use an ArrayList
instead, because that's an array that grows in size dynamically as more elements are added to it (in layman's terms).
如果您事先不知道数组的大小(例如,如果您正在读取文件并存储内容),您应该使用 anArrayList
代替,因为这是一个随着更多元素而动态增长的数组添加到它(用外行的话)。
回答by manjunath ramigani
You can do some thing like this,
你可以做这样的事情,
Initialize with empty array and assign the values later
用空数组初始化并稍后分配值
String importRt = "23:43 43:34";
if(null != importRt) {
importArray = Arrays.stream(importRt.split(" "))
.map(String::trim)
.toArray(String[]::new);
}
System.out.println(Arrays.toString(exportImportArray));
Hope it helps..
希望能帮助到你..