3

I know that you can't use a for loop to create multiple variables with numerically patterned names but is there any way I can simplify the following lines of code with some kind of loop? Otherwise it's a very long file that does very little, and I don't like that! Here's a shortened version of my code.

public DrawGrid(Context context, int[] binary) {...
sq00c = binary[0];  
sq01c = binary[1];  
sq02c = binary[2]; ...etc}

Rect sq00 = new Rect(); Paint sq00p = new Paint();  int sq00c;  
Rect sq01 = new Rect(); Paint sq01p = new Paint();  int sq01c;  
Rect sq02 = new Rect(); Paint sq02p = new Paint();  int sq02c; ...ect

protected void onDraw(Canvas canvas) {...
sq00.set(2*sqsize, line0, 3*sqsize, line0+sqsize);  sq00p.setColor(sq00c);  sq00p.setStyle(Paint.Style.FILL);   canvas.drawRect(sq00, sq00p);       
sq01.set(3*sqsize, line0, 4*sqsize, line0+sqsize);  sq01p.setColor(sq01c);  sq01p.setStyle(Paint.Style.FILL);   canvas.drawRect(sq01, sq01p);
sq02.set(4*sqsize, line0, 5*sqsize, line0+sqsize);  sq02p.setColor(sq02c);  sq02p.setStyle(Paint.Style.FILL);   canvas.drawRect(sq02, sq02p);
...etc}

Each of the three parts of code above occur 64 times each. Is there any way to simplify it with a loop? Thanks

6
  • 1
    Can't you use arrays? Commented Mar 29, 2013 at 16:06
  • I had read that in a similar question here but wasn't sure how to implement it in my situation. Commented Mar 29, 2013 at 16:08
  • i think that arrays or lists may help you, if yes i can put an example Commented Mar 29, 2013 at 16:08
  • An example would really help. Commented Mar 29, 2013 at 16:08
  • It's possible you're looking for a 2d array? Commented Mar 29, 2013 at 16:29

3 Answers 3

2

Try this way:

private int[] myInt;
private Rect[] myRect;
private Paint[] myPaint;

public DrawGrid(Context context, int[] binary) {
    myInt = binary;
    myRect = new Rect[myInt.length];
    myPaint = new Paint[myInt.length];

    for (int i = 0; i < myInt.length; i++) {
        //Put Rect parameters here, you have to take advantage of the "i" variable 
        myRect[i] = new Rect();

        myPaint[i] = new Paint();
        myPaint[i].setColor(myInt[i]);
        myPaint[i].setStyle(Paint.Style.FILL);
    }
}

protected void onDraw(Canvas canvas) {
    for (int i = 0; i < myRect.length; i++) { // < not >
        canvas.drawRect(myRect[i], myPaint[i]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This worked great, although you had > instead of < in the for loop and it took me ages to realise!
0

Keep an array of Rects, Paints, and ints (or, better yet, put them all in a nice, encapsulated object and keep an array of those ) and initialize them in a loop.

Comments

0

For example, try this:

Rect[] rects=new Rect[SIZE];

and see this link

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.