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.


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

Results 1 to 3 of 3

Thread: For loop in array

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Thanked 0 Times in 0 Posts

    Default For loop in array


    I have a question about a program i have to write.
    I have to build an hopsital. This hospital has an array of rooms. Each room contains an array of patients.
    The length of the patientarray is random, so I don't know how many patients i can put in an room.
    I have to know in which room there is room for an patient. Therefore i have to create an for loop. This for loop has to return in where there is an place available for the patient. I really don't know how to do that. Can anybody help me?
    I'm working with BlueJ.

    By the way, sorry for my bad English. Im from Holland.

    This is the sourcecode i have written yet. (kamer=room in dutch)

    public Kamer()
    Random generator = new Random();
    int randomIndex = generator.nextInt(5);
    int kamers [] = new int [randomIndex];
    kamertjes = kamers;

    public int getKamer()
    return kamertjes.length;

  2. #2
    Join Date
    Mar 2012
    United States
    My Mood
    Thanked 33 Times in 31 Posts

    Default Re: For loop in array

    Hello Mickeydus,

    Do you know how to loop through an array and just display the value at each index? If so, then it is just a matter testing the value with a simple if statement to see if that position is vacant.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 827 Times in 770 Posts
    Blog Entries

    Default Re: For loop in array

    See The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. Array Lists filling for loop
    By rob17 in forum Collections and Generics
    Replies: 9
    Last Post: March 22nd, 2012, 08:28 PM
  2. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 12:28 PM
  3. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 08:48 PM
  4. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM
  5. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM