0

I just want to Sort an array of Cards by its cardtype "one of four Spade, Heart, Club, Diamond" i.e Spades comes first

I made new array please solve it

private static void colorSort(Card temp[]){      
    Card arr[];             
    arr = new Card[13];            
    int loc=0;              
    for(Card x: temp){            
        if(x.cardType=="Spade"){           
            arr[0] = temp[x];    //this line giving error that "can't convert Card to                          int"`            
            loc++;           
        }    
    }   
}

2 Answers 2

1

You can't index an array by an object (the than an Integer) - you must use an int.

But you don't want to access the array, you want to use the object in the loop:

arr[0] = x;  
Sign up to request clarification or add additional context in comments.

Comments

0

Change

arr[0] = temp[x];

to

arr[loc] = x;

The for loop puts a card in x you don't need to access the array again.

You also need to place the values in a new index, limited by the array size, which is 13. if you do arr[0] you are always replacing the value in that index. So, put the values in a new index in the array with arr[loc]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.