This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Need help for the below array program

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



//getting arrayindexoutofbound exception
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74367
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic