0

I try to make a multidimensional array with methods. But i stuck in here; my print method not working properly. It shows nothing and no errors.

public void bas() { // ========> print method
for (int a = 0; a < dizi.length; a++) {
    for (int b = 0; b < dizi[a].length; b++) {
        System.out.println(" dizi[" + n + "][" + m + "] = "
        + dizi[n][m]);
    }}}

it fixed.

public int[][] doldur() { // ========> fill method
for (int i = 0; i < dizi.length; i++) {
    for (int j = 0; j < dizi.length[i]; j++) { //problem in here: The type of the expression must be an array type but it resolved to int

    }
}
return dizi;

}

dizi.length[i] => dizi[i].length


rest of my code:

public class ikiBoyutluDizi {
int n, m;
int[][] dizi = new int[n][m];

public int[][] diziBoyutu(int a, int b) {
    return dizi;
}

public int[][] doldur() { // ========> fill method
    for (int i = 0; i < dizi.length; i++) {
        for (int j = 0; j < dizi.length[i]; j++) {

        }
    }
    return dizi;
}

public void bas() { // ========> print method
    for (int a = 0; a < dizi.length; a++) {

        for (int b = 0; b < dizi[a].length; b++) {

            System.out.println(" dizi[" + n + "][" + m + "] = "

            + dizi[n][m]);

        }
    }
}

public static void main(String[] args) {
    ikiBoyutluDizi dizi2x = new ikiBoyutluDizi();
    dizi2x.diziBoyutu(2, 3);
    dizi2x.doldur();
    dizi2x.bas();
}

}

One more thing; can you check my print method? Do you think is it working properly after fix the fill method?

3
  • Just a question. You seem to do the print method correctly (except null check) so why do you do the fill method wrong? Both nested for loops are of the same logic. Commented Nov 12, 2012 at 22:50
  • Btw there are semantic errors in the rest of your code (before fill method and after print method). It won't work as per how I think you're intending it to work. Commented Nov 12, 2012 at 22:54
  • @ADTC because; inattention :) Commented Nov 12, 2012 at 22:58

2 Answers 2

2

In the problematic line - you should use dizi[i].length instead of dizi.length[i].

The reason is dizi is of type int[][] and thus dizi[i] is of type int[] - which is an array, but dizi.length is an int, and you actually tried to access an element in an int - which is impossible!

You might also want to make sure dizi[i] != null before doing so

public int[][] doldur() { // ========> fill method
for (int i = 0; i < dizi.length; i++) {
    for (int j = 0; dizi[i] != null && j < dizi[i].length; j++) { 
      //               ^                         ^
      //           not null                   dizi[i].length 
      //                                         instead of
      //                                      dizi.length[i]
      // Do domething

    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@android93: What is the problem with it? It seems fine to me, at least from first look.
@android93: Did you make sure n and m are set to values which are not 0? You should initialize them to a different value in the c'tor or you get an array of size 0 x 0, and the loops terminates before the first iteration (since 0 < 0 yields false)
I'd say his whole program is written with a wrong understanding of the basics of OOP. There are several errors, not just the initialization error you point out. I hope he's using Eclipse to write the code.
0
for (int j = 0; j < dizi[i].length; j++)

dizi[i] would get the array at i index. and length attribute would get its length.

Comments

Your Answer

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