Portál AbcLinuxu, 26. dubna 2024 07:41


Dotaz: Dedeni od ArrayList

7.8.2009 13:00 Adam Janota
Dedeni od ArrayList
Přečteno: 199×
Odpovědět | Admin

Dobry den, rad bych si vytvoril tridu zdedenou od tridy ArrayList, chci pridat pouze jednu metodu a to boolean metodu, ktera bude zjistovat, zda pro vsechny prvky seznamu plati

a[i]=i;

Zacal jsem takto:

public class ArrayListCustom Integer  extends ArrayList Integer 
{
    public ArrayListCustom()
    {
        super();
    }
    
    public boolean isLinear()
    {
    
    }
}

Ale v tele metody isLinear jsem se zasekl, protoze vlastne nevim, jak mohu pristupovat k prvkum toho seznamu. Muzete prosim poradit?

Nástroje: Začni sledovat (0) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

7.8.2009 13:15 anonym
Rozbalit Rozbalit vše Re: Dedeni od ArrayList
Odpovědět | | Sbalit | Link | Blokovat | Admin

neda se pro arraylist pouzit indexy a pak pristupovat ke kazdemu indexu zvlast?

7.8.2009 13:19 chearius | skóre: 7 | blog: /dev/chearius | Heidelberg
Rozbalit Rozbalit vše Re: Dedeni od ArrayList
Odpovědět | | Sbalit | Link | Blokovat | Admin

Resit se to da velmi jednoduse - deite od ArrayList, tudiz mate u objektu this pristup ke vsem public a protected metodam materske tridy. Implementace tridy s metodou isLinear muze vypadat treba takto:

public class ArrayListCustom extends ArrayList<Integer> {
    public static final long serialVersionUID = 1l;

    public ArrayListCustom() {
        super();
    }

    public boolean isLinear() {
        boolean result = true;

        for (int i = 0; i < this.size(); i++) {
            if (this.get(i) != i) {
                result = false;
                break;
            }
        }

        return result;
    }
}

btw. pokud neplanujete psat tu tridu genericky, ale jen pro jeden typ (Integer), je lepsi v nazvu tridy kompletne vynechat definici generickeho typu. Viz kod v prikladu.

S pozdravem, Marek Siller

7.8.2009 14:17 Adam Janota
Rozbalit Rozbalit vše Re: Dedeni od ArrayList

To je presne ono, dekuji:)

kouby avatar 7.8.2009 15:02 kouby | skóre: 27 | blog: init | Praha
Rozbalit Rozbalit vše Re: Dedeni od ArrayList

Trochu rychlejší řešení by mohla být iterace z obou stran. Pokud array list nabývá vetších rozměrů, tak je to i znatelné.

Nejrychlejší řešení je však vytvořit vlastní handle na metody add, get. Poté by zjištění vaší linearity bylo konstantní.

public class ArrayListCustom extends ArrayList<Integer> {
    public static final long serialVersionUID = 1L;

    public ArrayListCustom() {
        super();
    }

    public boolean isLinear() {
        for (int i = 0, size = this.size(), j = size - 1; i < size && j >= i; i++; j--) {
            if (this.get(i) != i || this.get(j) != j) {
                return false;
            }
        }
        return true;
    }
}

 

That's thirty minutes away. I'll be there in ten.

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.