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

Thread: Java abstracts

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java abstracts

    For the code belong, I'm trying to compare all the objects with each other and print if they're the same, is there any efficient methods in doing so beside me creating like 20 if else statement and compare each object individually? Thanks
    import java.util.*;
    public class TestHomework3
    {
        public static void main(String[]args)
        {
            List<Homework3>  workArray= new ArrayList<Homework3>();
            MyMath3 math = new MyMath3();
            MyScience3 science = new MyScience3();
            MyEnglish3 english = new MyEnglish3();
            MyJava3 java = new MyJava3();
            workArray.add(math);
            workArray.add(science);
            workArray.add(english);
            workArray.add(java);
     
            math.createAssignment(4);
            science.createAssignment(6);
            english.createAssignment(4);
            java.createAssignment(5);
     
            for(int x = 0 ; x < workArray.size();x++)
            {   
                System.out.println(workArray.get(x).toString());
            }
     
                if(workArray.get(0).compareTo(workArray.get(1)) == 0 )
                    System.out.println("The same");
                else
                    System.out.println("Differents");
     
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java abstracts

    Use an array or arraylist to hold the objects.
    Use a nested loop to compare each object in the array/arraylist against the rest of the objects in the array/arraylist.
    Last edited by Norm; March 26th, 2013 at 02:55 PM. Reason: added arraylist
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstracts

    Would the List that I created with the objects being added fine or do I need to create a different array? Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java abstracts

    A List should should work.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstracts

    This is the nested loop that I have but it also compare the objects to itself since x and i have the same value at some point. How can I avoid the objects inside the list comparing itself? Thanks
        for(int x = 0 ; x < workArray.size();x++)
            {   
                System.out.println(workArray.get(x).toString());
                for(int i = 0 ; i < workArray.size();i++)
                {
                    if(workArray.get(x).compareTo(workArray.get(i)) == 0 )
                        System.out.println(workArray.get(x).getType()+"The same"+workArray.get(i).getType());   
                }
            }
    The print out is
    "Math - must read 4 pages.
    MathThe sameMath
    MathThe sameEnglish
    Science - must read 6 pages.
    ScienceThe sameScience
    English - must read 4 pages.
    EnglishThe sameMath
    EnglishThe sameEnglish
    Java - must read 5 pages.
    JavaThe sameJava
    "
    You can see it the objects is comparing itself to itself.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java abstracts

    How can I avoid the objects inside the list comparing itself?
    To see how to control the indexes, take a piece of paper and draw a horizontal row of numbers to represent items in an array. Point one index to the first item and the second index to the next item. Move the second index to the next item etc as each are compared. The move the first index to the next item and look where the second index should start to do the next scan of compares to the end of the row.

    Hint: for loop indexing values do not have to start at 0.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstracts

    I understand the starting at another index part but let say I put i=1 instead of 0 , it might not compare itself to thing at index 0 but at index 1 and so on it will compare itself.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java abstracts

    Don't use hardcoded numbers.
    In my last post where should the second index start when the first index is moved to the next item? What is the relationship between where the first index is set to as it moves to the right and where the second index should start?
    If you can't see it, make a table with two columns, put the first index value in the first column and where the second index should start at in the second column. I'll give the first row:
    0 1
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstracts

    The second indexes should be 1 more than the first, am I correct?

    --- Update ---

    It work when I set i = x+1. Is this the correct way? Thanks

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java abstracts

    Yes that should do it. Remember to be careful to not run off the end of the list.
    If you don't understand my answer, don't ignore it, ask a question.