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

Thread: Need help with array comparison

  1. #1
    Banned
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with array comparison

    I need to create a method to check the elements of two arrays and determine if they are equal..


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

    Default Re: Need help with array comparison

    If the order matters as well, you can do it manually by iterating through each array comparing each value at position x, or using the Class Arrays equals function. If the arrays may be out of order you can do it manually by taking each value in the first array, and searching the second for that value, returning false if it cannot be found, or true at the end of the iteration if all were found. Alternatively, you could sort the arrays and use the Arrays object to compare

  3. #3
    Banned
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with array comparison

    The order is not important all, Here is what i have tried using but i get an illegal start to expression error
    for the if statement... Please help me set something up im really lost...



    boolean check = Arrays.equals(setA, setB);
    if (check == true){
    System.out.println("Arrays are same.");
    else
    System.out.println("Both Arrays are not same.");
    }

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

    Default Re: Need help with array comparison

    You are missing a pair of brackets in the code you posted. Be careful if order does not matter and you wish to check if each array contains the same SET of values (as opposed to SET and SEQUENCE), for example consider the following arrays:
    int array1[] = {1,2,3,4};
    int array2[] = {1,2,3,4};
    int array3[] = {2,1,3,4};

    Arrays.equals() will return true when comparing array1 and array2, but false when comparing array1 to array3 - even though thesy contain the same values (because they are out of order).
    If you are truly confused, I recommend going back to the basics
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  5. #5
    Banned
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with array comparison

    I understand that part. Now what if the elements within the array are read in by using a scanner(user input). I can i compare those elements which i do not know or are not fixed.

Similar Threads

  1. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  2. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  3. Array comparison
    By subhvi in forum Collections and Generics
    Replies: 5
    Last Post: September 3rd, 2009, 03:56 AM