-1

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. thanks any help

PrR3
  • 1,250
  • 1
  • 18
  • 26
uzjsme
  • 1
  • 1

4 Answers4

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

na is of size 0, it can has no elements at all. 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<Integer> na = new ArrayList<>();

And after filling it, you can easily convert it back to an array.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 1
    He doesn't know how many negatives are in the array. The problem is that he keeps redeclaring the array at each iteration. – Cruncher Mar 26 '14 at 16:36
  • But there could be at most `a.length` negatives. – Maroun Mar 26 '14 at 16:36
  • Thanks, but resulting array is of a.length size, should be na.size. – uzjsme Mar 26 '14 at 16:38
  • 1
    @MarounMaroun ArrayList is definitely the best solution. You can even set capacity to a.length so that it's guaranteed to never copy the array. Note with this, he still has to return an `int[]`. toArray will give an `Integer[]` and I'm not sure if java 7 will box that into an `int[]` – Cruncher Mar 26 '14 at 16:40
0

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 forna with count elements; then on the next line you are accessing element i of that array. It is the case that i will be >= count, so the access is out of bounds.

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].

davmac
  • 20,150
  • 1
  • 40
  • 68
0

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...

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.

At the end, instead of printing na.length just print out count. And if want the negative values, just in a for loop have count has you max condition.

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

Finally, I would suggest using an ArrayList instead of an array for na if possible.

Dan
  • 876
  • 5
  • 15
0

Here's a way to do it without worrying about what size your array is, and still returning an array of ints.

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;
}
Matthew Wilson
  • 2,045
  • 14
  • 27
  • Might as well pass a.length into the ArrayList constructor to guarantee the array will not copy. – Cruncher Mar 26 '14 at 16:47
  • This code needs a bit more work :( You can't convert an ArrayList of Integers to an array of primitive ints - http://stackoverflow.com/questions/718554/how-to-convert-an-arraylist-containing-integers-to-primitive-int-array – Matthew Wilson Mar 26 '14 at 16:49