JAVA Array:  Add

Posted on 2009-04-23
Last Modified: 2012-05-06

Dear experts:


I  would like to know whether I may objects to a panel using "for loop" as seen below.

Thanks a lot!

____________________   Want-to-try Code ________________

for ( int index = 0;  index < array.length; index++  )
     topPanel.add (button [index]);
   _________________   Current Code  _________________  
     topPanel.add  (  button [ 0 ] );
     topPanel.add  (  button  [ 1 ] );
     topPanel.add  (  button  [ 2 ] );
     topPanel.add  (  button  [ 3 ] );
Question by:duta
    LVL 92

    Expert Comment

    yes thoses two are equivalent (assuming size of array is 4)

    Author Comment

    Savant, thank you so much for your kind, prompt reply.

    Yes, they are the same. I wonder whether I may replace the lengthy second one with the first one.

    I tried but I got some error message.

    LVL 92

    Accepted Solution

    > for ( int index = 0;  index < array.length; index++  )

    should be:

    for ( int index = 0;  index < button.length; index++  )


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now