#index out of bounds

44 messages · Page 1 of 1 (latest)

mint bear
#
        this.antRader = antRader;
        this.antKolonner = antKolonner;
        rutene = new Celle[antRader][antKolonner];     
    }

    public void lagCelle(){
        Celle celle = new Celle();
        rutene[antRader][antKolonner] = celle;
        if(Math.random()<=0.3333 && rutene[antRader][antKolonner] != null){
            celle.settLevende();
        }
    }

    public void fyllMedTilfeldigeCeller(){
        for(int r=0;r<rutene.length;r++){
            for(int k=0;k<rutene.length;k++){
                if(rutene[r][k] == null){
                    lagCelle();
                }
            }
        }

 private static void testFyllMedTilfeldigeCeller () {
    Rutenett testRutenett = new Rutenett(10, 5);
    testRutenett.fyllMedTilfeldigeCeller();
``` im guessing something is wrong in the for loop? It says "Index 10 out of bounds for length 10" so to me it seems that when antRader = 10 somethign is out of bounds but idrk
plz help 🙂
halcyon skiffBOT
#

This post has been reserved for your question.

Hey @mint bear! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

mint bear
#

right

novel sage
mint bear
novel sage
mint bear
novel sage
#

because antRader == 10, but max index is 9, since count starts from 0

novel sage
mint bear
#
        Celle celle = new Celle();
        rutene[antRader-1][antKolonner-1] = celle;
        if(Math.random()<=0.3333 && rutene[antRader-1][antKolonner-1] != null){
            celle.settLevende();
        }
    }```
#

its the if statement

#

i changed it to -1

#

and now i get "Index 5 out of bounds for length 5"

novel sage
#

dunno, should work

mint bear
#

yeah i think so too

#

but the if statement is no good apperantly

novel sage
#

maybe you reset the values of antRader and antKolonner somewhere? or recreate the array?

#

anyways, it should work for sure
if(Math.random()<=0.3333 && rutene[rutene.length-1][rutene[rutene.length-1].length-1] != null){

mint bear
#
    public int antRader;
    public int antKolonner;
    Celle[][] rutene;

    public Rutenett(int antRader, int antKolonner){
        this.antRader = antRader;
        this.antKolonner = antKolonner;
        rutene = new Celle[antRader][antKolonner];     
    }```did i maybe define something wrong?
novel sage
#

everything looks ok

mint bear
#

ffs

#

im so lost then

novel sage
mint bear
#

this is mine

#

and then

#

2secs

#

this is the testfile

#

and each test method is commented out at the bottom

#

so just remove the //

#

so its like this

novel sage
#

Celle class?

mint bear
#

oh shit yeah 2secs

#

this is also written by me, but i am almost 100% sure that this works as its supposed to

novel sage
#

first 2 tests are passed

#

testHentCelle test and rest had to comment since there is no hentCelle method defined in Rutenett class

mint bear
#

but holy shit

#

i would have never seen that

#

thank yuo

#

you

#

although now rutene[0][0] != null fails