Results 1 to 2 of 2
  1. #1
    hara is offline Member
    Join Date
    Apr 2016
    Posts
    3
    Rep Power
    0

    Default Need help for the below array program

    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

    class ArrayComparision
    {
    public static void main(String[] b)
    {

    int i[]={10,20,3,2,4};
    int j[]={5,2,15,16,18};

    int k[]= new int[5];
    int ni=0;

    for(int t:i)
    {
    int c=0;
    for(int r:j)
    {
    if (t==r)
    {
    c++;
    }
    }
    if(c==0)
    {
    ni++;
    }
    }

    k= new int[ni];
    System.out.println(k.length);

    //int[] nk=null;
    int nii=0;

    for(int r=0;r<i.length;r++)
    {
    for(int t=0;t<j.length;t++)
    {
    int x=0;
    if(i[r]==j[t])
    {
    x++;
    }
    if(x==0)
    {

    k[nii]=i[r];
    nii++;

    }
    }
    }


    }
    }


    //getting arrayindexoutofbound exception

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help for the below array program

    In the future, please place your code between code tags. Read about it here.
    Java Code:
    class ArrayComparision {
       public static void main(String[] b) {
          int i[] = { 10, 20, 3, 2, 4
          };
          int j[] = { 5, 2, 15, 16, 18
          };
    
          int k[] = new int[5];
          int ni = 0;
    
          for (int t : i) {
             int c = 0;
             for (int r : j) {
                if (t == r) {
                   c++;
                }
             }
             if (c == 0) {
                ni++;
             }
          }
    
          k = new int[ni];
          System.out.println(k.length);
    
          // int[] nk=null;
          int nii = 0;
    
          for (int r = 0; r < i.length; r++) {
             for (int t = 0; t < j.length; t++) {
                int x = 0;
                if (i[r] == j[t]) {
                   x++;
                }
                if (x == 0) {
                   k[nii] = i[r];
                   nii++;
                }
             }
          }
    
       }
    }
    In your second set of nested loops, you increment nii every time x == 0. So nii can get too big (try printing it out after the loops
    terminate). You need to comment out the k[nii] = i[r] to see this problem.

    A few observations:

    1. You don't use 'c' so get rid of it
    2. You reallocate k without having first used it.
    3. You really only need one set of nested loops (and another single loop to create a "pure" array of the numbers).

    Regards,
    Jim
    Last edited by jim829; 05-03-2016 at 02:22 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. 2D array program
    By shaq in forum New To Java
    Replies: 21
    Last Post: 03-30-2011, 10:53 PM
  2. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  3. Help with an Array program please?
    By EternalSolitude in forum New To Java
    Replies: 3
    Last Post: 10-30-2008, 03:39 AM
  4. Array program help
    By adelgado0723 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:19 PM
  5. Need help with my 1st array program
    By Phobos0001 in forum New To Java
    Replies: 5
    Last Post: 03-22-2008, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •