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 4 of 4

Thread: Counting Matches in Parallel Arrays

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Counting Matches in Parallel Arrays

    I want to make a method involving arrays.
    Both are Integer Arrays. (And are the same size, so I don't need to check if it's the same size)
    Its needs see how many of the parallel positions matches there are.
    I know what's needed, but don't know how to really put it together.
    int[] arrayone=new int[10];
    int[] arraytwo=new int[10];

    The code above I do not want in the method.

    public static void Count(int?? confused hear)
    {

    if (arrayone[i] ==arraytwo[i])
    variable=true;

    some accumulator might be needed

    return match;
    }

    What I have above might be on the right track, but wrong in terms of implementation.
    Noob in Java. Still trying to get a feel for methods and arrays.

    What I have above doesn't count the number of times the values in the corresponding positions are the same; it just indicates if the value in the corresponding position is the same. Not sure how to count it.

    Any suggestions would be great.

    Forgot to add the for loop but don't worry about that part.
    Last edited by dx8292; January 31st, 2012 at 10:58 PM. Reason: forgot detail


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    My Mood
    Lurking
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Counting Matches in Parallel Arrays

    Something like this?

    public static void main(String[] args) {
    int[] arrayone = new int[10];
    int[] arraytwo = new int[10];
    int matches=0;
    matches = count(arrayone,arraytwo);
    System.out.println(matches);
    }

    public static int count(int[] a, int[] b) {
    int match = 0;
    for (int i = 0; i < a.length; i++) {
    if (a[i] == b[i])
    match++;
    }
    return match;
    }
    }

  3. The Following User Says Thank You to rchrispink3913 For This Useful Post:

    dx8292 (February 1st, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Counting Matches in Parallel Arrays

    I had something different in mind, but just by looking it seems right. I'll play around with the code tomorrow cause I'm tired right now, too much coding today, but good practice. I'll post back tomorrow after I finish some Visual Basic stuff and let you know how it worked out, but either way THANK YOU, I appreciate it.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Counting Matches in Parallel Arrays

    @rchrispink3913, please read the forum rules as well as the following:
    The Problem with Spoon-feeding

Similar Threads

  1. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 07:43 PM
  2. Seraching for String highlight all matches
    By fortune2k in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 2nd, 2010, 01:33 PM
  3. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  4. Replies: 2
    Last Post: March 4th, 2009, 06:32 AM
  5. Replies: 1
    Last Post: December 30th, 2008, 07:30 AM