0

I have two classes. block and main
public Block(Stage stage) {
super(stage);
//fill array
where[t][s]=i;
i++;
system.out.println(where[t][s]);

}
and the main.
the problem is I create an instance of the class block and I know it runs because I put a system.out there and the I a list of printed numbers in a dos window.
the problem is in the main class I call it like
system.out.println(Block.where[0][2]);
and the output is "0";
????
why is this?
AND HOW to solve?
thanks for looking

3
Contributors
4
Replies
5
Views
11 Years
Discussion Span
Last Post by ultimate_fusion
0

Try using "getters" and "setteres":

public class Block
{
       private int[][] where;
       
       public Block(Stage s)
       {
              super(s);
              fillArray(4,4);
       }
 
       public void fillArray(int firstIndex, int secondIndex)
       {
                  where = new int[firstIndex][secondIndex];
                  int x = 0; 
                  for (int i=0; i<firstIndex; i++)
                  {
                          for (int j=0; j<secondIndex; j++)
                          {
                                  where[i][j] = x;
                                  x++;
                          }
                 }
       } 

      [B] public int getElement(int firstIndex, int secondIndex)
       {
               return where[firstIndex][secondIndex];
       }[/B]
}


------Main Class-----------

public class Test
{
        public static void main(String[] args)
        {
                 Block b = new Block(StageName);
                 System.out.println(b.getElement(0,2));
        }
}
0

By the way, I don't understand the Stage part either. Unless you are extending a class that takes a Stage as an argument, you don't need that...It does look pretty useless though.

0

the stage class is needed, it was simpler just to tell you as little as possible.
that getters and setters is what I was looking for.
I need a little change to it which I cant solve.
instead of one object which fills the array
I need lots of objects and them to keep filling the next array block.
I try'd

public Block(Stage stage) {
   super(stage);
           if (s==4){s=0; t++;}
           c=c+10; 
           s++;
           fillArray(t,s,c);
}
public void fillArray(int t, int s, int c)
   {
    System.out.println(s);
    System.out.println(c);
         where[t][s]=c;
         System.out.println(where[t][s]);
    } 

but the system.out's always say "0"
I think its becuase every time I create a new object it calls the default construct so t,s,c will equal 0 again?
how to fix?

Edited by pyTony: fixed formating

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.