Initialize reference variables for many objects?

Zach Dziura :

I'm having a little trouble building the grids for a Battleship game for my Java class. So far, I can easily make a for loop to add JPanel or JButton objects to the JFrame. However, my issue is that I'll need to use those Panels or Buttons again when playing the game (such as clicking on a button to see if your opponent put a ship on that square, et cetera). Is there a simple way in Java to initialize reference variables for a LOT of objects? Or will I have to declare all of them individually?

willcodejavaforfood :

You could try a multi dimensional array of JPanels (or any other object). Create an array with the same size as your grid. The line below initializes an array with 5 rows and 5 columns.

JPanel[][] battleField = new JPanel[5][5];

Use nested for loops to create the panels in the array.

for (int rowIndex = 0; rowIndex < battleField.length; rowIndex++)
{
    for (int cellIndex = 0; cellIndex < battleField[rowIndex]; cellIndex++)
    {
         battleField[rowIndex][cellIndex] = new JPanel();
    }
}

If you want to reference the battleField array later on you would just make it into a instance variable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reference variables and objects elsewhere in a form

From Dev

How to initialize objects in Java - with fields or with local variables?

From Dev

How to reference many objects at once in Actionscript?

From Dev

Undefined reference: trying to initialize pointer array of objects from different class

From Dev

Relevel many variables with given different reference level using for loop in r

From Dev

Making mutable variables accessible across many, otherwise-unconnected objects

From Java

Does Java GC destroy objects if instance variables still have reference?

From Dev

Using Object variables to reference new data types/objects

From Dev

In Objective-C, How to initialize NSMutableArray with objects in another NSMutableArray using tableView reference

From Dev

Initialize rvalue reference member

From Dev

how to initialize a constexpr reference

From Dev

Initialize vector passed by reference

From Dev

Many objects under objects

From Java

Initialize objects with Lombok

From Dev

Initialize objects in Blazor component

From Dev

Initialize array of objects in constructor

From

How to initialize static variables

From Dev

Initialize variables in constructor or in declaration

From Java

Automatically initialize instance variables?

From Dev

AngularJS initialize controller variables

From Dev

How to initialize variables in an LSTM?

From Dev

Initialize const member variables

From Dev

Always initialize variables in C

From Dev

Initialize integer variables in CyLP

From Dev

Initialize and call variables

From Dev

Updating one-to-many reference field in Django also updates other objects

From Dev

Django - initialize many to many fields on database level

From Dev

Initialize member reference with default value

From Java

How to initialize a reference variable in Kotlin

Related Related

  1. 1

    Reference variables and objects elsewhere in a form

  2. 2

    How to initialize objects in Java - with fields or with local variables?

  3. 3

    How to reference many objects at once in Actionscript?

  4. 4

    Undefined reference: trying to initialize pointer array of objects from different class

  5. 5

    Relevel many variables with given different reference level using for loop in r

  6. 6

    Making mutable variables accessible across many, otherwise-unconnected objects

  7. 7

    Does Java GC destroy objects if instance variables still have reference?

  8. 8

    Using Object variables to reference new data types/objects

  9. 9

    In Objective-C, How to initialize NSMutableArray with objects in another NSMutableArray using tableView reference

  10. 10

    Initialize rvalue reference member

  11. 11

    how to initialize a constexpr reference

  12. 12

    Initialize vector passed by reference

  13. 13

    Many objects under objects

  14. 14

    Initialize objects with Lombok

  15. 15

    Initialize objects in Blazor component

  16. 16

    Initialize array of objects in constructor

  17. 17

    How to initialize static variables

  18. 18

    Initialize variables in constructor or in declaration

  19. 19

    Automatically initialize instance variables?

  20. 20

    AngularJS initialize controller variables

  21. 21

    How to initialize variables in an LSTM?

  22. 22

    Initialize const member variables

  23. 23

    Always initialize variables in C

  24. 24

    Initialize integer variables in CyLP

  25. 25

    Initialize and call variables

  26. 26

    Updating one-to-many reference field in Django also updates other objects

  27. 27

    Django - initialize many to many fields on database level

  28. 28

    Initialize member reference with default value

  29. 29

    How to initialize a reference variable in Kotlin

HotTag

Archive