954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to check if elements are same in the array?

i have this dataset:

String[][] trainData= {
           //featIdx////////classInfo
            {"s", "y", "r"},//0
            {"s", "n", "r"},//1
            {"w", "y", "r"},//0
            {"r", "y", "p"},//0
            {"r", "n", "r"},//2
            {"r", "y", "p"},//0
            {"w", "n", "p"},//0
            {"w", "n", "r"},//3
            {"w", "y", "r"},//0
            {"s", "n", "r"}};//1
int[] trainClassInfo = {0, 1, 0, 0, 2, 0, 0, 3, 0, 1};

i want to check if the elements of trainClassInfo is all same at the indexes when trainData[i][0]=="s"
i wrote this method but it doesnt perform correctly, i'm confused ... :confused:

public boolean allSame(int featIdx, String featVal)
    {
        boolean allSame = false;
        int classInfo =-1;

            for(int i=0; i<trainData.length; i++){
                if(trainData[i][featIdx]==featVal){//when trainData element equals "s"
                    classInfo = trainClassInfo[i];//save classInfo temporarily
                }
            }

            for(int i=0; i<trainData.length; i++){
                if(trainData[i][featIdx]==featVal && trainClassInfo[i]==classInfo )
                    allSame = true;
                else 
                    allSame = false;
            }

        return allSame;
    }

it must return FALSE when i put this method like this: allSame(0, "s") , but it returns TRUE

gingerfish
Light Poster
47 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

to check for the equality of objects, you don't use the '==' operator, but you use the .equals method.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

As stultuske pointed out you use equals that you either have to write or you can reuse something that already exists. For example Arrays. equals(Object[] a, Object[] a2)

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

I suspect the OP problem is much simpler; it is a purely logical error. Opon the first loop completion, classInfo is the value of the last qualifying trainClassInfo. The second loop always runs all the way through, and the last comparison always evaluates to True.
Solution: exit the second loop as soon as the mismatch is detected.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

The OP should try debugging his code to see what is happening. If no interactive debugger available, add printlns to the code to show the execution flow and how the values of variables change.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

i figured it out! thanks all :D

gingerfish
Light Poster
47 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: