找到整数数组的长度/大小

时间:2022-01-16 22:17:36

I have an integer array int[] a = new int [5].

我有一个整数数组int [] a = new int [5]。

In my code I am storing only 2 values at indices 0 and 1.

在我的代码中,我只在索引0和1处存储了2个值。

a[0]=100 and a[1]=101

a [0] = 100且a [1] = 101

Now I need to get the array size/length as 2.

现在我需要将数组大小/长度设置为2。

What I should do?

我该做什么?

5 个解决方案

#1


9  

You array length is 5, not 2. You've defined your array to be 5 elements long, how many you ended up using is irrelevant.

数组长度为5,而不是2.您已将数组定义为5个元素长,最终使用的数量是无关紧要的。

What can you do instead is this:

你能做什么呢:

List<Integer> a = new ArrayList<Integer>();
a.add(100);
a.add(101);
System.out.println(a.size());

will give you 2

会给你2

#2


6  

You can't - there's no difference between an element which hasn't been set and an element which has been set to 0. The actual length of the array is 5, and will always be 5. (Arrays can't change in length after creation.)

你不能 - 没有设置的元素和设置为0的元素之间没有区别。数组的实际长度是5,并且总是为5.(数组的长度不能改变创作之后。)

Of course, if you know that you'll never use 0, you could write:

当然,如果你知道你永远不会使用0,你可以写:

int size = 0;
for (int value : a)
{
    if (value != 0)
    {
        size++;
    }
}

... but if you're trying to use the array as a buffer with a "live" segment at the start (like ArrayList does) then you'll have to maintain that size yourself.

...但是如果您尝试在开始时将数组用作具有“实时”段的缓冲区(就像ArrayList那样),那么您必须自己维护该大小。

#3


1  

You can not change the size of the array. However, you can make a new array with the correct size and copy the data you are interested in into the new array.

您无法更改阵列的大小。但是,您可以创建具有正确大小的新数组,并将您感兴趣的数据复制到新数组中。

#4


1  

You could loop through the array and check the last index that isn't 0 - or if you use the Integer type you could do the same check but check for null instead of 0. But this won't give you the length, it's merely a poor estimate for how many values you've used.

您可以循环遍历数组并检查最后一个非0的索引 - 或者如果您使用Integer类型,您可以执行相同的检查但检查null而不是0.但这不会给您长度,它只是您使用了多少值的估计不佳。

A far better way would be to use an arraylist and then get the size of that. You're most likely using arrays for the wrong purpose here.

一个更好的方法是使用arraylist然后获得它的大小。你最有可能在这里使用数组用于错误的目的。

#5


1  

Use ArrayList or Vector .

使用ArrayList或Vector。

Vector intvec = new Vector();
intvec.add(100);
intvec.add(101);
System.out.println(intvec.size());

#1


9  

You array length is 5, not 2. You've defined your array to be 5 elements long, how many you ended up using is irrelevant.

数组长度为5,而不是2.您已将数组定义为5个元素长,最终使用的数量是无关紧要的。

What can you do instead is this:

你能做什么呢:

List<Integer> a = new ArrayList<Integer>();
a.add(100);
a.add(101);
System.out.println(a.size());

will give you 2

会给你2

#2


6  

You can't - there's no difference between an element which hasn't been set and an element which has been set to 0. The actual length of the array is 5, and will always be 5. (Arrays can't change in length after creation.)

你不能 - 没有设置的元素和设置为0的元素之间没有区别。数组的实际长度是5,并且总是为5.(数组的长度不能改变创作之后。)

Of course, if you know that you'll never use 0, you could write:

当然,如果你知道你永远不会使用0,你可以写:

int size = 0;
for (int value : a)
{
    if (value != 0)
    {
        size++;
    }
}

... but if you're trying to use the array as a buffer with a "live" segment at the start (like ArrayList does) then you'll have to maintain that size yourself.

...但是如果您尝试在开始时将数组用作具有“实时”段的缓冲区(就像ArrayList那样),那么您必须自己维护该大小。

#3


1  

You can not change the size of the array. However, you can make a new array with the correct size and copy the data you are interested in into the new array.

您无法更改阵列的大小。但是,您可以创建具有正确大小的新数组,并将您感兴趣的数据复制到新数组中。

#4


1  

You could loop through the array and check the last index that isn't 0 - or if you use the Integer type you could do the same check but check for null instead of 0. But this won't give you the length, it's merely a poor estimate for how many values you've used.

您可以循环遍历数组并检查最后一个非0的索引 - 或者如果您使用Integer类型,您可以执行相同的检查但检查null而不是0.但这不会给您长度,它只是您使用了多少值的估计不佳。

A far better way would be to use an arraylist and then get the size of that. You're most likely using arrays for the wrong purpose here.

一个更好的方法是使用arraylist然后获得它的大小。你最有可能在这里使用数组用于错误的目的。

#5


1  

Use ArrayList or Vector .

使用ArrayList或Vector。

Vector intvec = new Vector();
intvec.add(100);
intvec.add(101);
System.out.println(intvec.size());