Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How would I iterate through an array 'in chunks'?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default How would I iterate through an array 'in chunks'?

    Hello all, I've been working on tile generation for my game, and it uses 2d arrays. I was trying to think of some way to do biome/chunk generation, and I thought that looping through an array in chunks instead of looping straight through it. If you don't understand, here is what I am trying to do:

    for(int t = 0; t < World.MAP_WIDTH; t += 36) {
    	for (int x = 0 + t; x < 36 + t; x++) {
    		for (int y = 0 + t; y < 36 + t; y++) {
    			genTiles(x, y, w, 1);
    		}
    	}
    }

    What I thought it would do is for the first loop (when t is 0) genTiles() from x 0 y 0 to x 35 y 35. Then get shifted over when that was done, so it started at x 36 y 36, ended at x 71 y 71. But I realized it skipped a few 'chunks'. Is it possible to iterate over an array in chunks but still counting every slot in the array?
    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How would I iterate through an array 'in chunks'?

    Is it possible to iterate over an array in chunks but still counting every slot in the array?
    Are you asking if you can iterate through a loop using an increment value other than 1 for the control variable?
    Yes. It will be easier if the increment value doesn't change too often.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How would I iterate through an array 'in chunks'?

    I *think* what you are trying to do is iterate over an array by considering that array to be made up of smaller subarrays. (Correct me if I've got it skipped a few 'chunks' wrong). Consider:

    aaabbbcccddd
    aaabbbcccddd
    eeefffggghhh
    eeefffggghhh
    iiijjjkkklll
    iiijjjkkklll

    It's an array 12 wide and 6 tall, but can be considered as an array of chunks 4 wide and 3 tall where each chunk is also an array (3 wide and 2 tall). You can iterate over each of the large array elements with:

    int width = 4; // number of chunks across
    int height = 3 // number of chunks tall
    int chW = 3; // width of a chunk
    int chH = 2; // height of a chink
    for(int chunkX = 0; chunkX < width; ++chunkX) {
        for(int chunkY = 0; chunkY < height; ++chunkY) {
            for(int x = 0; x < chW; ++x) {
                for(int y = 0; y < chH; ++y) {
                    System.out.printf(
                            "At (%d,%d) within the chunk at (%d,%d)",
                            x, y, chunkX, chunkY);
                    doSomething(chunkX * width + x, chunkY * height + y);
                }
            }
        }
    }

    (My preference is to make the loop variables "simple" and do the calculation of overall "coordinates" with the loop body. I guess initialising the loops with a more complex expression might be more efficient.)

    The four for loops are a bit ungainly. If the chunks have some significance within your program, consider making a class to represent them. A class which has, as behaviour, the things that chunks can do. The array then presents itself in a straightforward way as a 2D array of chunks.

Similar Threads

  1. How to split a text file in no of chunks ?
    By kewlkeny in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: September 3rd, 2013, 06:51 AM
  2. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  3. Iterate over array, changing current object?
    By slimchance in forum Loops & Control Statements
    Replies: 7
    Last Post: May 14th, 2012, 04:52 PM
  4. [SOLVED] How to Iterate through a string?
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 8
    Last Post: November 2nd, 2010, 05:45 PM
  5. Reading Chunks of Data
    By icu222much in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 22nd, 2010, 08:39 PM