Comparing array values


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Comparing array values

  1. #1
    Join Date
    Feb 2005
    Posts
    6

    Comparing array values

    I am writing a program in my high school java class in which i have to create two arrays of different lengths and have different numbers, but share some. I need to know how i can write the code to make java go through and pick out and print the numbers that the two arrays share. Thanks for help on this code.

  2. #2
    Join Date
    Jan 2005
    Posts
    38
    Hello
    i will give the idea and if you cannot do it please tell me >>>

    frist you have to choose (big or small) array ...
    then you have to compare the first element in the (big or small) array with EVERY element in the another one ((by using nested loops)) if it is there you have to increment the variable which is intially equals to zero>>> this variable is there to decide the length of the new array that you want to add the sharing numbers to it...

    After you assign the length of the new array, you have to repeat the same nested loops to take the sharing number and put it in the newer array...

    I hope that is clear...

    regards,
    Last edited by LuisFigo; 02-26-2005 at 04:56 AM.

  3. #3
    Join Date
    Jan 2005
    Posts
    38
    this is My code >>>

    i advise you to solve it by own but if you cannot you can use my code ...

    Code:
    import java.io.*;
    public class Sharing
    {
    	public static void main(String[]args) throws IOException
    	{
    		InputStreamReader s = new InputStreamReader(System.in);
    		BufferedReader stdin = new BufferedReader(s);
    		
    		System.out.print("Eneter the Size of the First Array: ");
    		int array1_size = Integer.parseInt(stdin.readLine());
    		System.out.print("Eneter the Size of the Second Array: ");
    		int array2_size = Integer.parseInt(stdin.readLine());
    		
    		int [] array1 = new int[array1_size];
    		int [] array2 = new int[array2_size];
    		
    		for (int i=0;i<array1_size;i++)
    		{
    			System.out.print("Eneter the "+i+" Element of the First Array: ");
    			array1[i]=Integer.parseInt(stdin.readLine());
    		}
    		
    		for (int i=0;i<array2_size;i++)
    		{
    			System.out.print("Eneter the "+i+" Element of the Second Array: ");
    			array2[i]=Integer.parseInt(stdin.readLine());
    		}
    		
    		int array3_size=0;
    		for(int i=0;i<array1_size;i++)
    		{
    		
    			for (int j=0;j<array2_size;j++)
    			{
    				if(array1[i]==array2[j])
    				array3_size++;
    			}
    		}
    		
    		int[]array3 = new int[array3_size];
    		int k=0;
    		for(int i=0;i<array1_size;i++)
    		{
    		for (int j=0;j<array2_size;j++)
    			{
    				if(array1[i]==array2[j])
    				{
    				array3[k]=array1[i];
    				k++;
    				}
    			}
    		}
    		System.out.print("[ ");
    		for (int i=0;i<array3_size;i++)
    		System.out.print(array3[i]+" ");
    		System.out.print("]"+"\n");
    	}
    }
    regards,

  4. #4
    Join Date
    Feb 2005
    Posts
    6

    thanks

    thanks for the help very much appreciated.

  5. #5
    Join Date
    Mar 2005
    Posts
    5

    question

    i was looking at this code and wondered if it was possible to do this with file input if so how thank you

  6. #6
    Join Date
    Jan 2005
    Posts
    38
    Yes, you can!
    but i need to know what is the arrangment of the file???
    if it is like this:
    4
    5
    6
    .
    .
    .
    you can use readLine() method and you have to count the number of lines to be the size of the array>>>

    OR,

    Maybe it's like this:
    1 2 3 4 . . .
    then, you have to use string tokenizer and use countTokens() to specify the size of an array>>>

    if you want me to do this for you, you have to tell me about the arrangment of the file and how many files do you want to use >>> is it Two files one for an array>>>


    Regards,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center