5 Replies - 317 Views - Last Post: 04 June 2014 - 06:32 PM Rate Topic: -----

#1 leo255  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-November 13

Want to copy matching numbers from two arrays onto a new one

Posted 04 June 2014 - 04:05 PM

Hi all,

Just studying for my Java final. No homework here. I want my function to return an array, with the array holding just the values of data that appear in good.

This is what should be returned:

{3, 5, 3, 2, 3, 3}

What is currently being returned:

{0, 3, 5, 3}

I didn't want to miss any numbers, so I decided to iterate through j for the "good" array, and then just i for the one that I was looking for matching numbers. Perhaps part of the problem is that if the condition is met, it goes to the next iteration of the loop. I'm not sure.

Thanks for looking!

public class Arrays2 {
	
	public static void main(String args[]){
		
		int [] data = {8, 3, 5, 3, 7, 2, 8, 3, 3 }; 
		int [] good = {5, 2, 3, 2};

		
		int [] result = f2(data, good);		
		
		for (int i = 0; i < result.length; i++){
			
			System.out.println(result[i]);
			
			
		}
	}

	public static int[] f2(int []data, int[] good){
		
		int [] result = new int[good.length];
		
		for (int i = 0; i < good.length; i++){
		
			for (int j = 0; j < good.length; j++){
			
			if (good[j] == data[i])
				result[i] = data[i];			
			
			}
			
		}
		
		return result;
		
	}
	
}

This post has been edited by leo255: 04 June 2014 - 04:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Want to copy matching numbers from two arrays onto a new one

#2 drjoliv  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 60
  • Joined: 27-August 13

Re: Want to copy matching numbers from two arrays onto a new one

Posted 04 June 2014 - 04:17 PM

Your question is a bit confusing? Can you rephrase it? Are you trying to find matching numbers in the two arrays?
Was This Post Helpful? 0
  • +
  • -

#3 leo255  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-November 13

Re: Want to copy matching numbers from two arrays onto a new one

Posted 04 June 2014 - 04:28 PM

I'll just post how my professor wrote it:

public static int[] f2( int[] data, int[] good );
f2's job is to create and return an array that holds just the values of data that appear in good. f2 may not change either arg array.
For instance, if data were {8, 3, 5, 3, 7, 2, 8, 3, 3 } and good were {5, 2, 3, 2}, then f2 would create and return {3, 5, 3, 2, 3, 3}. (That second 2 in the good array doesn't change anything.)
Was This Post Helpful? 0
  • +
  • -

#4 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 645
  • Joined: 24-April 10

Re: Want to copy matching numbers from two arrays onto a new one

Posted 04 June 2014 - 04:50 PM

 for (int i = 0; i < good.length; i++){

this is only going to iterate the length of good, which is what the inner loop is doing. I believe this loop should be < data.length

Also
int [] result = new int[good.length];
What if the data was 5,2,3,2,5,2,3,2,5,2,3,5,2? ALL of these should be included, so good.length is not long enough. Perhaps data.length makes more sense.

This post has been edited by mike73: 04 June 2014 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 drjoliv  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 60
  • Joined: 27-August 13

Re: Want to copy matching numbers from two arrays onto a new one

Posted 04 June 2014 - 06:24 PM

Try this code out.


import java.util.ArrayList;
import java.util.Arrays;

public class DreamInCode {
  

    public static int[] f2(int[] data, int[] input) {
        
       
        ArrayList<Integer> result = new ArrayList<>();
            
        Arrays.sort(input);
        ArrayList<Integer> listInput = new ArrayList<>(input.length);
         for(int b : input )
                listInput.add(B)/>;
         
         for(int i = 0 ; i < listInput.size(); i++)
                if( (i+1 < listInput.size()) &&  listInput.get(i).equals(listInput.get(i+1)))
                    listInput.remove(i+1);
         

       
        for (Integer i : listInput) 
            for (int h : data) 
                if (i == h) 
                    result.add(h);
             
        if(result.isEmpty())
            return null;
        
        int[] arrayResult = new int[result.size()];
        
            for(int e = 0; e < result.size(); e++){
                arrayResult[e] = result.get(e);
            }
        return arrayResult;
        
    }
    
    
    public static void main(String [] args){
        int [] data = {8, 3, 5, 3, 7, 2, 8, 3, 3 };
        int [] good = {5, 2, 3, 2};

        System.out.println(Arrays.toString(f2(data, good)));
    }

}

This post has been edited by drjoliv: 04 June 2014 - 06:25 PM

Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 862
  • View blog
  • Posts: 2,650
  • Joined: 29-July 11

Re: Want to copy matching numbers from two arrays onto a new one

Posted 04 June 2014 - 06:32 PM

Saying "it's not homework", doesn't convince anyone that it is not. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1