• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help for the below array program  RSS feed

 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i[]={10,20,3,2,4};
int j[]={5,2,15,16,18};

need a new array which will be having elements based on if 'array i' will be compared to 'array j'

Ex:
10 will be compared against 'array j'and and so on ,Only unmatched elements index will be the elements of new array
for 10,20,3,4 will be unmatched so their indexes will be printing into another array

output :
{0 1 2 4}

please help me solve this
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, have you learned how to use loops yet? You will need to get somewhat comfortable with them, as you probably need to nest them for the solution. Second, have you learned the collections library yet? It will probably be easier to use a Java collection, and then convert it to an array, as you probably won't know how elements in the result to start.

Henry
 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



//getting arrayindexoutofbound exception
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A note about your post: please use code tags when posting your code. I've done it for you this time. Follow the link if you have any questions.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: when you get an error, post the complete error message.

About your code: you are using two loops so that you can find the size of the array, but Lists will grow to any size. Try using an ArrayList and one loop, then converting the List into an array if need be.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haraprasad mohapatra wrote:int i[]={10,20,3,2,4};
int j[]={5,2,15,16,18};
. . . .
output :
{0 1 2 4}
. . .
In Java® always put the [] as part of the type. It should read int[] array not int array[]. The fact that you can get such code past the compiler does not make it good code
I find it understand how you get from two 5‑element arrays to one 4‑element array. Why is the 2 matched? Does it mean there is a 2 in the other array at any location? I presume this is an exercise in using nested loops. If so, please use variable names which people can understand. Things like nii are very difficult to understand.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output array is the set of indices that contain numbers unique to the i array. Thus the 2 is there because the "3" at index 2 is unique. Same for 0 (the "10" at index 0 is unique), 1 (the "20" at index 1 is unique), and 4 (the "4" at index 4 is unique).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!