简体   繁体   English

创建负整数数组

[英]create array of negative integers

trying to put all negative ints in array 试图将所有负整数放入数组

int[] a = {1,2,3,-4,-5,-5,-9};

into separate array, this code produces 'array out of bounds' not sure why 分成单独的数组,此代码会产生“数组超出范围”,不确定为什么

    int[] negatives(int[] a){
    int count = 0;
    int[] na = new int[0];
    for(int i=0;i<a.length;i++){
        if(a[i]<0){
            count++;
            na=new int[count];
            a[i] = na[i];
        }
    }

         System.out.print(na.length);//<-shows correct size for resulting array

    return na;
}

console output for na.length gives correct size. na.length的控制台输出给出正确的大小。 thanks any help 谢谢你的帮助

int[] na = new int[0];
                   ↑

na is of size 0, it can has no elements at all. na的大小为0,它根本不能包含任何元素。 Do: 做:

int[] na = new int[a.length];

And do this outside the method. 并在方法之外执行此操作。

If you want the sizes to be the same, you have to use an ArrayList instead: 如果希望大小相同,则必须使用ArrayList

ArrayList<Integer> na = new ArrayList<>();

And after filling it, you can easily convert it back to an array. 填充后,您可以轻松地将其转换回数组。

Although your code looks wrong in several ways, I believe these are the lines that are causing the exception: 尽管您的代码在几种方面看起来是错误的,但我相信这些是导致异常的行:

        na=new int[count];
        a[i] = na[i];

You are creating an array for na with count elements; 您正在为带有count元素的na创建一个数组; then on the next line you are accessing element i of that array. 然后在下一行上,您正在访问该数组的元素i It is the case that i will be >= count , so the access is out of bounds. 在这种情况下, i将> = count ,因此访问超出范围。

Also worth noting is that the array you create is uninitialised, so even if the index were not out of bounds, you are effectively just assinging a 0 to a[i] . 另外值得注意的是,您创建的数组尚未初始化,因此即使索引没有超出范围,您实际上也只是将0赋给a[i]

Kind of hit upon in the previous answer, reallocating a new array per new negative integer found is not efficient at all, especially considering you need to copy over the "old" na values into the newly allocated na to keep a running total... 在上一个答案中有点类似,每个发现的新负整数重新分配新数组根本没有效率,尤其是考虑到您需要将“旧” na值复制到新分配的na中以保持运行总数...

To answer your question, to avoid the out of bounds exception it should be, 为了回答您的问题,为避免出现超出范围的异常,

na[count-1]=a[i];

But, like the previous comment alluded to, create an array first equal to the size of "a" and then you can just add elements as needed without having to create a new array and copy over the elements. 但是,就像前面提到的注释一样,首先创建一个等于“ a”大小的数组,然后可以根据需要添加元素,而不必创建新的数组并复制到元素上。

At the end, instead of printing na.length just print out count. 最后,不是打印na.length而是打印出count。 And if want the negative values, just in a for loop have count has you max condition. 而且,如果要为负值,则仅在for循环中具有count即可。

for(int j=0; j< count; j++)
//....

Finally, I would suggest using an ArrayList instead of an array for na if possible. 最后,我建议尽可能使用ArrayList而不是数组。

Here's a way to do it without worrying about what size your array is, and still returning an array of ints. 这是一种不用担心数组大小而仍返回int数组的方法。

int[] negatives(int[] a){

   ArrayList<Integer> negatives = new ArrayList<Integer>();

    for(int i=0;i<a.length;i++){
        if(a[i]<0){
            negatives.add(a[i])
        }
    }

    int[] na= negatives.toArray(new int[negatives.size()]);
    return na;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM