如何在java中找到整数数组的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28806092/
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
How to find integer array size in java
提问by Shiva Krishna
below is my code which is throwing error:
下面是我抛出错误的代码:
Cannot invoke size() on the array type int[]
Code:
代码:
public class Example{
int[] array={1,99,10000,84849,111,212,314,21,442,455,244,554,22,22,211};
public void Printrange(){
for (int i=0;i<array.size();i++){
if(array[i]>100 && array[i]<500)
{
System.out.println("numbers with in range ":+array[i]);
}
}
Even i tried with array.length()
it also throwing the same error. When i used the same with string_name.length()
is working fine.
即使我尝试使用 array.length()
它也抛出相同的错误。当我使用相同时,string_name.length()
它工作正常。
Why it is not working for an integer array?
为什么它不适用于整数数组?
采纳答案by Shiva Krishna
The length of an array is available as
数组的长度可作为
int l = array.length;
The size of a List
is availabe as
a 的大小List
可用作
int s = list.size();
回答by Ruchira Gayan Ranaweera
There is no method call size()
with array
. you can use array.length
有没有方法调用size()
用array
。您可以使用array.length
回答by Ankur Singhal
Array's has
数组有
array.length
whereas List has
而 List 有
list.size()
Replace array.size()
to array.length
替换array.size()
为array.length
回答by Arun Kumar
public class Test {
int[] array = { 1, 99, 10000, 84849, 111, 212, 314, 21, 442, 455, 244, 554,
22, 22, 211 };
public void Printrange() {
for (int i = 0; i < array.length; i++) { // <-- use array.length
if (array[i] > 100 && array[i] < 500) {
System.out.println("numbers with in range :" + array[i]);
}
}
}
}
回答by Suman Behara
Integer Array doesn't contain size() or length() method. Try the below code, it'll work. ArrayList contains size() method. String contains length(). Since you have used int array[], so it will be array.length
整数数组不包含 size() 或 length() 方法。试试下面的代码,它会起作用。ArrayList 包含 size() 方法。字符串包含长度()。由于您使用了 int array[],因此它将是 array.length
public class Example {
int array[] = {1, 99, 10000, 84849, 111, 212, 314, 21, 442, 455, 244, 554, 22, 22, 211};
public void Printrange() {
for (int i = 0; i < array.length; i++) {
if (array[i] > 100 && array[i] < 500) {
System.out.println("numbers with in range" + i);
}
}
}
}
回答by Varun
we can find length of array by using array_name.length attribute
我们可以使用 array_name.length 属性找到数组的长度
int [] i = i.length;
int [] i = i.length;
回答by munmunbb
I think you are confused between size() and length.
我认为您对 size() 和 length 感到困惑。
(1) The reason why size has a parentheses is because list's class is List and it is a class type. So List class can have method size().
(1) size之所以有括号,是因为list的class是List,是一个class类型。所以 List 类可以有方法 size()。
(2) Array's type is int[], and it is a primitive type. So we can only use length
(2) Array 的类型是 int[],是原始类型。所以我们只能使用长度