Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: help: union of two array,.. kinda problem

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question help: union of two array,.. kinda problem

    hello guys..
    i'm new here.
    i am trying to do a function that unite two arrays.

    this is my code:

    arr4 = interSection = gets the intersection of the two arrays.

    arr1,arr2 = similar... = reduce the repetition..

    i don't see "errors" in the code itself but when i run it.. i get this:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    	at Even.union(Even.java:64)
    	at Even.main(Even.java:33)

    which is this line (64):
    			arr3[arr1.length+i]=arr2[i-1];


    	public static int[] union(int[] arr1,int[] arr2) {
     
     
    		int[] arr4 = interSection(arr1,arr2);
     
    		arr1 = Similar(arr1);
    		arr2 = Similar(arr2);
     
    		int[] arr3 = new int[arr1.length+arr2.length-arr4.length];
     
    		for (int i=0;i < arr1.length;i++)
    				arr3[i]=arr1[i];
     
    		for (int i=1;i<=arr2.length;i++)
    			arr3[arr1.length+i]=arr2[i-1];
     
     
     
    		return Similar(arr3);
     
    	}


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: help: union of two array,.. kinda problem

    You always need to be careful when you play with array indexes, especially if you use the plus sign when calculating the index.

    arr1.length+i

    This piece of code above will most likely cause an issue if you're not 100% sure of what the value can be.

    You call a method interSection which does what?

    The things you need to be aware of is the length of each array and if the interSection is returning an array thats too big then the length of arr3 will be too short because you use arr4 when initialising arr3 and its length.

    I'm sorry I can't be much more of help but if you'd like a better way of adding one array onto another then give us a shout.

    // Json

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help: union of two array,.. kinda problem

    well.. thanks for the reply..
    i already had it..

    	public static int[] union(int[] arr1,int[] arr2) {
     
    		int[] arr3 = new int[arr1.length+arr2.length];
     
    		System.arraycopy(arr1, 0, arr3, 0, arr1.length); 
    		System.arraycopy(arr2, 0, arr3, arr1.length, arr2.length);
     
    		return arr3;
     
    	}

Similar Threads

  1. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  2. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM
  3. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  4. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM