• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in Array value copy  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I am trying to copy all odd value of an array in one array and all even value in another array

this what i have try to so far



But i am getting error

Any suggestion where I am wrong?


Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One piece of advice: whenever you're asking a question about an error you're getting...tell people what the error is. In detail (error messages usually contain a lot of useful information). Otherwise you're asking them to waste time trying to work out things that you already know.

So, is it a compiler error? If so, what is the message, and where does it happen? If it's an exception, what is it, and which line is it thrown from? If it's not doing what you expected, what is it doing, and what did you expect? Tell us that and we can probably help.

But one thing you may want to consider in this case: Java arrays have a fixed length. Once created, you can't make them longer.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks metthew

I appoloige for incomplete information

ok here is my array

int a[]={11,6,2,9,1,44,29,3,19};

I want to copy all odd value into an new array "old_value" and all even value to "even_value" array.

and when i run program i am getting error on this block.


else
{
odd_value[i]=a[i];
}


I am getting error when i click run button.


And Exception is ArrayIndexOutOfBounds Exception


 
Bobby Sveinheldt
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you never assigned the arrays a length -- when you create them, you're going to want to do something like this:

int odd_value[] = new int[n];

int even_value[] = new int[n];

instead of:
int odd_value[];

where n is the length that you want odd_value and even_value to be. With your code, the compiler thinks they have no size, which means that when you go through your for loop it will think you are trying to access spots that aren't in your array (since it thinks there are no spots), which causes the exception to be thrown.

Hope this helps,
Bobby
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, how big is the odd_value array? Does it contain the index that you're trying to use there? If you look at where you initialise the odd and even arrays, you'll see you're actually creating zero length arrays, which means you can't add anything to them.

This would actually be easier if you were using Lists rather than arrays, because Lists are designed to have a flexible size. But arrays are fixed size - if you want to use an array, the first thing you need to do is work out how big it needs to be.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok here is few changes I made in my program



and I am getting this output


I want only odd value to be in "odd_value" and even value in "even_value" array
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your main problem now is that the j and k variables are defined within the loop. So on line 12, for example, j is always zero. That's why you're only ending up with a single value in each array - you're always updating the first element.

Once you've fixed that, you'll still have some spare zeros at the end, because you've made each array to be the maximum length they ever might need to be (if all numbers are even, for instance), rather than the actual length required. You can either work out in advance how big they need to be (so loop through the input twice), or use a List instead as I suggested.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew

I defined value of j,k outside for loop.

Working fine as I am expected thanks

Sorted out
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!