This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-10-05 07:32:23

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Java problem

So, I have some code like this:

Code:

ArrayList<Class<? extends Piece>> ap = stage.getApplicablePieceList();
for(int i = 0; i < ap.size(); i++){
  Class<? extends Piece> currentPiece = ap.get(i);
  //Problem here, see rest of post
}

Where the comment is, I want to access some static variables of the Piece subclass stored in currentPiece. How would I go about this?
(Notes:
- Piece is a class I coded, the source of it is not currently applicable
- I'm not sure if size() is the right method to get the size of an ArrayList, but I don't have the code on me (I'm at school) so assume it is)


/* No comment */

Offline

 

#2 2011-10-05 07:45:29

ZeroLuck
Scratcher
Registered: 2010-02-23
Posts: 500+

Re: Java problem

1. the size() method is right.
2. If you want to access static variables you only have to write:
Piece.myStaticVariable


http://3.bp.blogspot.com/-oL2Atzp0Byw/T465vIQ36dI/AAAAAAAAADo/1vqL4PvhkM0/s1600/scratchdachwiki.png

Offline

 

#3 2011-10-05 12:24:42

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: Java problem

ZeroLuck wrote:

1. the size() method is right.
2. If you want to access static variables you only have to write:
Piece.myStaticVariable

From my general knowledge of coding, I would think that the above is correct


http://i.imgur.com/zeIZW.png

Offline

 

#4 2011-10-06 12:46:02

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: Java problem

What I mean is, currentPiece loops through each subclass of Piece stored in ap. I need to access static variables of the class currently in currentPiece. currentPiece.staticVar doesn't seem to work.


/* No comment */

Offline

 

#5 2011-10-06 13:12:26

ZeroLuck
Scratcher
Registered: 2010-02-23
Posts: 500+

Re: Java problem

I have an example for you:

Code:

class Main {
    public static void main(String[] args) {
        // set currentPiece to 0
        Piece.currentPiece = 0;
        // print the current value
        System.out.println("currentPiece is: "+Piece.currentPiece);
        // change the currentPiece
        Piece.currentPiece += 1;
    }
}
class Piece {
    public static int currentPiece;
}

http://3.bp.blogspot.com/-oL2Atzp0Byw/T465vIQ36dI/AAAAAAAAADo/1vqL4PvhkM0/s1600/scratchdachwiki.png

Offline

 

#6 2011-10-09 16:06:57

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Java problem

Well, I'm an absolute n00b at Java, but isn't the thing *.length instead of size()?  Sorry if this refers to something totally different, I couldn't really read the code and I have no idea whatsoever about what an ArrayList is (assuming it's an array).

Offline

 

#7 2011-10-09 16:11:27

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: Java problem

maxskywalker wrote:

Well, I'm an absolute n00b at Java, but isn't the thing *.length instead of size()?  Sorry if this refers to something totally different, I couldn't really read the code and I have no idea whatsoever about what an ArrayList is (assuming it's an array).

indeed you're right


http://i.imgur.com/zeIZW.png

Offline

 

#8 2011-10-10 08:45:33

ZeroLuck
Scratcher
Registered: 2010-02-23
Posts: 500+

Re: Java problem

Code:

Object[] array = new Object[100];
System.out.println(array.length); // use length for arrays.
ArrayList list = new ArrayList();
for(int i = 0;i < 100;i++){
    list.add("#"+i);
}
System.out.println(list.size()); // use size() for lists

Last edited by ZeroLuck (2011-10-10 08:46:17)


http://3.bp.blogspot.com/-oL2Atzp0Byw/T465vIQ36dI/AAAAAAAAADo/1vqL4PvhkM0/s1600/scratchdachwiki.png

Offline

 

#9 2011-10-10 21:11:18

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: Java problem

Try this:

Code:

for (Object piece : stage.getApplicablePieceList())
{
    System.out.println(((Piece)piece).staticVar);
}

Oh, and make sure staticVar is defined in Piece or it won't compile.

Last edited by MathWizz (2011-10-10 21:25:36)


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

Board footer