Java – how to sort an array composed of “null” and a string?

I have an array of 20:

private Karte[] deckArr;
deckArr = new Karte[20];

Now I want to sort the array by name every time I add a new card

Attachment: after clicking a button, the cards will be added one by one, so there are spaces in the array

Since

Arrays.sort(deckArr.getName());

... the method doesn't work here. I asked myself how I did it

Carter (card):

package Model;

/**
 * Created by 204g07 on 18.03.2016.
 */
public class Karte implements ComparableContent<Karte>{
    private int schoenheit;
    private int staerke;
    private int geschwindigkeit;
    private int intelligenz;
    private int coolness;
    private int alter;
    private String seltenheit;
    private String name;

    public Karte(String pName,int pSchoenheit,int pStaerke,int pGeschwindigkeit,int pIntelligenz,int pCoolness,int pAlter,String pSeltenheit ) {
        name=pName;
        schoenheit=pSchoenheit;
        staerke=pStaerke;
        geschwindigkeit=pGeschwindigkeit;
        intelligenz=pIntelligenz;
        coolness=pCoolness;
        alter=pAlter;
        seltenheit=pSeltenheit;
    }
    //getter
    public int getSchoenheit(){
        return schoenheit;
    }
    public int getStaerke(){
        return staerke;
    }
    public int getGeschwindigkeit(){
        return geschwindigkeit;
    }
    public int getIntelligenz(){
        return intelligenz;
    }
    public int getCoolness(){
        return coolness;
    }
    public int getAlter(){
        return alter;
    }
    public String getSeltenheit(){
        return seltenheit;
    }
    public String getName(){
        return name;
    }


    //setter
    public void setSchoenheit(int pSchoenheit){
        schoenheit = pSchoenheit;
    }
    public void setStaerke(int pStaerke){
        staerke = pStaerke;
    }
    public void setGeschwindigkeit(int pGeschwindigkeit){
        geschwindigkeit = pGeschwindigkeit;
    }
    public void setIntelligenz(int pIntelligenz){
        intelligenz = pIntelligenz;
    }
    public void setCoolness(int pCoolness){
        coolness = pCoolness;
    }
    public void setAlter(int pAlter){
        alter = pAlter;
    }
    public void setSeltenheit(String pSeltenheit){
        seltenheit = pSeltenheit;
    }
    public void setName(String pName){
        name = pName;
    }

    @Override
    public boolean isLess(Karte karte) {
        if (getName().compareTo(karte.getName()) < 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean isEqual(Karte karte) {
        return getName() == karte.getName();
    }

    @Override
    public boolean isGreater(Karte karte) {
        if (getName().compareTo(karte.getName()) > 0){
            return true;
        }else{
            return false;
        }
    }




}

Any help is appreciated!

Solution

Why not use ArrayList directly? It's easier to add and remove elements, and you'll never have empty slots

To sort anyway, you can use collections like this sort:

deckArr = new ArrayList<Karte>();
Collections.sort(deckArr,Comparator.comparing(karte -> karte.getName()));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>