Java 声明 int[] 数组而不定义大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20763347/
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
Declaring int[] Array without defining size?
提问by Guy
This is my code:
这是我的代码:
int[] primes = new int[25];
for (int i = 2; i <= 100; i++)
{
for (int j = i; j > 0; j--)
{
int prime = i%j;
if (prime == 0)
{
if ((j == i) || (j == 1))
{
isPrime = true;
}
else
{
isPrime = false;
break;
}
}
}
if (isPrime == true){
primes[index] = i;
index++;
}
}
As you can see, I'm writing prime numbers into array. I counted all the prime numbers that are within range 2-100, so I set array size to 25.
如您所见,我正在将质数写入数组。我计算了 2-100 范围内的所有素数,因此我将数组大小设置为 25。
But let's say that I wouldn't know there are 25 prime numbers. Is there a way to create an array (any type of array) without defining the size? I tried this:
但是假设我不知道有 25 个素数。有没有办法在不定义大小的情况下创建数组(任何类型的数组)?我试过这个:
int[] primes = new int[0];
But it crashed.
但它崩溃了。
采纳答案by BobTheBuilder
回答by MGorgon
List
s are best for situations, when you don't know exact size of array. For example, you can use ArrayList
.
List
s 最适用于您不知道数组的确切大小的情况。例如,您可以使用ArrayList
.
回答by thestar
You have to use List instead of an Array.
您必须使用 List 而不是 Array。
Compiler needs to know the size of an Array. When you define an Array like this. int[] primes = new int[0];
编译器需要知道数组的大小。当你像这样定义一个数组时。int[] 素数 = new int[0];
It creates space for only 1 integer. So, when you write to primes[1], it creates segmentation fault signal and your program crashes.
它仅为 1 个整数创建空间。因此,当您写入 primes[1] 时,它会创建分段错误信号并且您的程序崩溃。
回答by bstempi
No; an array's size must be declared ahead of time. Try taking a look at the different implementations of List
or Vector
.
不; 必须提前声明数组的大小。尝试查看List
or的不同实现Vector
。
If in the end you absolutely need an array, you may create an array from your List
.
如果最后你绝对需要一个数组,你可以从你的List
.
回答by Eran
As mentioned by others, you can use an ArrayList
, and if you still need the output as an array, you can convert the ArrayList
to array at the end (once you know the size of the array).
正如其他人所提到的,您可以使用ArrayList
, 并且如果您仍然需要将输出作为数组,则可以ArrayList
在最后将其转换为数组(一旦您知道数组的大小)。
回答by Manish Singh
Arrays are meant for the situation when we know about the size and number of elements we are going to put inside.If we are not sure about the size then in that case Collection API of Java can help you out. for example
数组适用于我们知道要放入的元素的大小和数量的情况。如果我们不确定大小,那么在这种情况下 Java 的 Collection API 可以帮助您。例如
List Interface implemented by ArrayList and Vector
ArrayList 和 Vector 实现的 List 接口
ArrayList can extends its size 50% at runtime so this will solve your problem.Or else you can use Vector if your application dealing with thread safety.but avoid using Vector because It increases its size to double at runtime.
ArrayList 可以在运行时将其大小扩展 50%,因此这将解决您的问题。否则,如果您的应用程序处理线程安全,您可以使用 Vector。但避免使用 Vector,因为它会在运行时将其大小增加一倍。
回答by pise
This code may help you.
此代码可能对您有所帮助。
public static void main(String[] args) {
boolean isPrime;
List<Integer> list = new ArrayList<Integer>();
for (int i = 2; i <= 100; i++)
{
isPrime = true;
for (int j = 2; j < i/2; j++)
{
int prime = i % j;
if (prime == 0)
{
isPrime = false;
break;
}
}
if (isPrime == true){
list.add(i);
}
}
// all the prime numbers
for(Iterator<Integer> iterator = list.iterator(); iterator.hasNext();){
System.out.print(iterator.next()+" ");
}
}