Results 1 to 8 of 8
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default Filtering an array

    Im trying to make a method to filter an array

    say array1 has these contents 10 20 30

    and array2 has 11 30

    the array values are entered by the user so both arrays can be any length

    The method would take in these two arrays then return a new array, which has the contents of array1 plus any new numbers from array2. So the returned array would be 10 20 30 11

    I can write the main method to take an array of any length from the user but im having trouble actually filtering the array.

    My idea was to get array2[i] from a for loop and test this against each content of array1 then move on to array[i+1]

    how ever I'm having trouble implementing this. Is there anyone that could help with the way to make this method?

    thanks in advance
    Last edited by counterfox; 10-15-2010 at 06:45 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Do you have to use arrays? If you'd use a few collections (Sets) the code would be just a few lines.

    kind regards,

    Jos

  3. #3
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    yeahh its like a homework sheet thing for university about arrays and this is the only question i'm stuck on :(

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by counterfox View Post
    yeahh its like a homework sheet thing for university about arrays and this is the only question i'm stuck on :(
    Ok, that's a fact then ;-) Can any of the arrays contain duplicate values? i.e. A = { 1, 2, 2, 3 }, B = { 3, 3, 4, 5, 6 }, result = { 1, 2, 3, 4, 5, 6 }?

    kind regards,

    Jos

  5. #5
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    Yes the arrays can contain duplicate values, however when the new array to returned is made it must start with the contents of the first array then any new values from the second array are added onto the new array so the result here would be:

    1 2 2 3 4 5 6

    Regards,

    James

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by counterfox View Post
    Yes the arrays can contain duplicate values, however when the new array to returned is made it must start with the contents of the first array then any new values from the second array are added onto the new array so the result here would be:

    1 2 2 3 4 5 6
    Ok, so given arrays A and B you can copy all the elements of A to the result while you have to check every element of B whether or not it is present in A. If it is don't do anything, if it isn't copy it to the result. Note that you don't know the size of the result array in advance, all you know that it is less or equal to the sizes of arrays A and B.

    kind regards,

    Jos

  7. #7
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    Thanks,

    One thing im confused with is for example as we don't know the sizes of either array until after the user has entered it I found its hard to calculate in the method first how to get the size of the new array.. Heres what I thought

    1.) I should find out how big the new array should be.

    So what I thought was to see how many new numbers (from array B) would be in the result array.

    From this i made a local variable which is a counter = 0; then in a for loop had a test against a.length
    //i is the variable in first for loop, j is the variable in the second for loop
    Then made nested loop in this where now i could test if a[i] != b[j] then do something like increment the counter as a new number would be in the new array.

    So this would test a[0] against b[0] then b[i] etc until the second for loop is finished. This is what I thought. However I could not get this to work or don't know if im missing something basic or overcomplicating things.

    2.) I would now create the new array
    int resultArr[] = new int [a.length+counter];

    Then from this fill out the array with array A then do another similar test like the 1st step to fill in the result array and return it.

    This was my initial idea but im not sure if its overcomplicating a more easy solution but I cant implement it and its driving me mad.

    Regards,

    James

  8. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Filtering an array

    Maybe this help...
    array1 also can be of any length, same as array2, and this example is with fixed number of elements.
    Java Code:
    public class FilteringArray {
        public static void main(String[] args) {
            //create array1
            int[] array1 = {1, 3, 2, 7, 5, 3, 2, 7, 4};
            //create array2 of length between 1 and 10
            int[] array2 = new int[(int)(1 + Math.random() * 10)];
            //fill array2 with random numbers between 1 and 10
            for (int i = 0; i < array2.length; i++)
                array2[i] = 1 + (int)(Math.random() * 10);                                             
    
            //create array of array1 and array2
            int[] array = new int[array1.length + array2.length];
            System.arraycopy(array1, 0, array, 0, array1.length);
            System.arraycopy(array2, 0, array, array1.length, array2.length);
            
            int[] finalArray = filterArray(array);
                    
            //print arrays
            System.out.println("array1:");
            for (int i = 0; i < array1.length; i++) 
                System.out.print(array1[i] + " ");
            System.out.println();
            
            System.out.println("array2:");
            for (int i = 0; i < array2.length; i++) 
                System.out.print(array2[i] + " ");
            System.out.println();
    
            System.out.println("array1 + array2:");
            for (int i = 0; i < array.length; i++) 
                System.out.print(array[i] + " ");                
            System.out.println();
            
            
            System.out.println("Array result:");
            for (int i = 0; i < finalArray.length; i++) 
                System.out.print(finalArray[i] + " ");                
            System.out.println();
        }
        
        public static int[] filterArray(int[] a) {
            //count different
            int count = a.length;//suppose all different
            start:
            for (int i = 0; i < a.length; i++)
                for (int j = i + 1; j < a.length; j++)
                    if (a[i] == a[j]) {
                        count--;
                        continue start;                    
                    }                                        
                        
            //create filtered array
            int[] filteredArray = new int[count];     
            count = 0;
            start:
            for (int i = 0; i < a.length; i++) {
                for (int j = i + 1; j < a.length; j++)
                    if (a[i] == a[j])   
                        continue start;       
            filteredArray[count] = a[i];
            count++;
            }
            return filteredArray;        
        }
        
        
    
        
    }
    Last edited by diamonddragon; 01-19-2012 at 05:01 AM.

Similar Threads

  1. Mouse event filtering?
    By Taiko in forum AWT / Swing
    Replies: 12
    Last Post: 08-10-2011, 03:00 PM
  2. JLists - Filtering
    By Psyclone in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2010, 07:09 AM
  3. looping and filtering
    By javafanatic in forum New To Java
    Replies: 14
    Last Post: 02-09-2010, 10:48 AM
  4. filtering data in jsf
    By bbq in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 07-04-2007, 09:52 AM
  5. Jsf, Filtering Data In A Table
    By Freddie in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-11-2007, 01:59 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
  •