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 2 of 2

Thread: Arrays and loops

  1. #1
    Junior Member
    Join Date
    Mar 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrays and loops

    When you're setting up the constructor, the constructor expects not only how many rows and columns you have, but also an already made two-dimensional array, or a table for example. Since we want to construct a grid in the constructor, it's best to get rid the pattern[][] from the constructor, leaving rows and columns untouched.

    There's also no need to have an emptyGrid instance since you'll be creating a table everytime you call the constructor.

    To iterate through an array in this case, you can use a for loop, which will start with an integer variable, i, equal to 0, because arrays counts its elements starting from 0. Since you have a set number of rows and columns, you can use those to make an expression whether the variable, i, is less than the number of columns or rows, and then after the execution of the for loop is finished, you can simply increment variable i by one.

    The for loop looks something like this:
    for (int i = 0; i < 5; i++){
    //code
    }

    For this code, we'll be using two for loops since the array has two dimensions.

    For setting a number inside of an array, you can use the first and the second variable of an array to set a number in a specific place inside of an array.

    array[i][j] = //any number you want

    Then afterward create a method that will get the grid instance so that all classes can access it.

    Finally, to obtain all the elements inside of arrays that have more than one dimension, you can call this to get all of elements.

    Arrays.deepToString(array);

  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: Help Please GameOfLife loop help

    Do you have any specific java programming questions?
    Be sure to wrap all posted code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Loops and arrays
    By mstratmann in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2013, 11:22 AM
  2. Tic-Tac-Toe (loops, arrays, and methods) help please
    By CVL220 in forum Collections and Generics
    Replies: 3
    Last Post: November 10th, 2012, 01:47 PM
  3. Distance formula with arrays, and loops. help?
    By smith999 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2012, 06:27 PM
  4. [SOLVED] Java assignment, concerning Arrays and Loops
    By trejorchest in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 17th, 2011, 12:59 PM
  5. Grades project/arrays, loops
    By rochla16 in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2011, 12:26 AM